I'm trying to make an active record that involves location. I get the longitude from the params, and try to use the Float() method to convert the locationLongitude and locationLatitude from a string to a float, and I get the following error:
undefined method `call' for #<Class:0x007ffead288530>
Here are the params that the method has access to:
{"locationName"=>"Stanford",
"locationLatitude"=>"37.42839679991957",
"locationLongitude"=>"-122.17553785073696"}
And this is the method in my controller that attempts to convert the strings into floats and make the query:
def local
radius = 10;
#sort = "local"
#locationName = params[:locationName]
#locationLongitude = Float(params[:locationLongitude])
#locationLatitude = Float(params[:locationLatitude])
#musings = Musing.(:longitude => (Float(#locationLongitude) - radius)..(Float(#locationLongitude) + radius))
end
Hope you can help. I also tried doing params[:locationName].to_f and that also didn't work.
Thanks, Paul.
I'd say it's better to move the processing from within your local method to the Musing (or other) model.
In your form - try to namespace your parameters such that it'd have a musing as an outer most one.
<input name='musing[locationName' ...>
In the controller
def local
# set some vars
#musings = Musing.search(params[:musing])
end
In the model
def self.search(params)
radius = 10
long = params[:locationLongitude]
lat = params[:locationLatitude]
return self unless long and lat
self.where(:latitude => lat.to_f-radius).where(:long => long.to_f-radius)
end
I can see you resolved the issue - but this might help
Please, change
params[:locationName].to_f
to
params[:locationName].to_s.to_f
The issue was in this line:
#musings = Musing.(:longitude => (Float(#locationLongitude) - radius)..(Float(#locationLongitude) + radius))
I wrote Musing.(...) instead of Musing.where(...)
What a scrub.
Related
I read all posts but don't find solution. I make a .near request with geocoder, and It work for one controller but doesn't work for another one with this error : Wrong number of arguments (3 for 1).
Here is the two same lines :
This one doesn't work in my school_controller.rb
#schools_premium = School.where(:subscription.exists => true)
#schools_aside = #schools_premium.near([params[:latitude], params[:longitude]], radius, units: :km).limit(3)
I try too with : #school.coordinates.reverse instead of params[:latitude] ..
But this one does (in home_controller.rb) :
#schools = School.near([params[:latitude], params[:longitude]], radius, units: :km).limit(30)
I have the geocoded_by and reverse_geocoder on my School model.
Does someone have the same issue ?
You can achieve by this way also:
#schools = School.near([params[:latitude], params[:longitude]], radius, units: :km).limit(30)
#schools_aside = #schools.premium_school
School.rb (model)
scope :premium_school, -> {where("subscription = ?", true)}
it is the first time to encounter this error in rails and i really don't know where is it coming from here is the function implementation :
def self.send (sender,recivers,content)
recivers.each do |reciver|
#notification = Notification.new
#notification.sender= sender
#notification.user = reciver
#notification.body = content
#notification.save
end
end
and here is how am calling it :
def after_create(announcment)
instructor_id = announcment.course.instructor_id
sender = User.find_by_id(instructor_id)
students = announcment.course.users
body = announcment.announcment
coures_name = announcment.course.name
Notification.send(sender,students,body)
UserMailer.notify_students_course(students,coures_name)
end
send is a Ruby method from Object class (so every class inherits it) that calls the method named with first argument and parameters the rest of the arguments given to send. Also in RoR its a reserverd word.
Better not overriding it, because it can get messy.
when trying to create a class with a single argument, I am getting the following error: NameError: undefined local variable or method radius' for #<Circle:0x007fdcda2b75c8> from circle_constructor.rb:13:ininitialize'
class Circle
def initialize(circle_constructor = {})
circle_constructor = {#radius => radius, #diameter => diameter}
#radius = radius
#diameter = diameter
end
end
if i understand you correctly you try to initialize #radius and #diameter with the hash you got as a parameter so try the following:
class Circle
def initialize(circle_constructor = {})
## circle_constructor = {#radius => radius, #diameter => diameter}
## you are inserting wrongly values to local variable inside constractor wothout doing nothing with it so remove it or do this:
## #circle_constructor = { radius: radius, diameter: diameter }
## make sure you populate radius and diameter local variables before.
#radius = circle_constructor[:radius]
#diameter = circle_constructor[:diameter]
end
end
Ruby doesn't support this out of the box, but you can use Hashie library for this. It has several hash variants and dash would work well for this.
require 'hashie/dash'
class Circle < Hashie::Dash
property :radius, required: true
property :diameter, required: true
end
# USAGE
Circle.new(radius: 10, diameter: 5)
(On a side note, it's odd that your circle accepts both a radius and a diameter. Unless it's user input such as a quiz, you should be able to derive one from the other, so it would normally only have one constructor param and the other would be a method.)
I'd like to ask a little question. It's rather trivial I guess but I might just look for the wrong solutions to my issue so I cant get it working.
I have a model called request.rb which has a method called self.dates
def self.dates
from_date = Request.last.date.to_date
to_date = Request.last.todate.to_date
weekdays = (from_date..to_date).select { |d| (1..5).include?(d.wday)}.count
weekend_days = (from_date..to_date).select { |d| [0, 6].include?(d.wday)}.count
end
I have another model called hotels.rb where I'd like to call the variables weekdays and weekend_days for the price calculation.
def self.best_deal_nm
weekday_nm_tot = (Request.dates.weekdays * pricea.wdpricenm) + (Request.dates.weekdays * pricea.wepricenm)
weekend_nm_tot = (Request.dates.weekend_days * priceb.wdpricenm) + (Request.dates.weekend_days * priceb.wepricenm)
[weekday_nm_tot, weekend_nm_tot].min
end
Unfortunately the code above doesnt work. My question is therefore how can I possibly call these two variables in my hotel model.
Thanks a lot for help
Just return all info in last line into your self.dates method like
def self.dates
from_date = Request.last.date.to_date
to_date = Request.last.todate.to_date
weekdays = (from_date..to_date).select { |d| (1..5).include?(d.wday)}.count
weekend_days = (from_date..to_date).select { |d| [0, 6].include?(d.wday)}.count
{'from_date' => from_date, 'to_date' => to_date, 'weekdays' => weekdays, 'weekend_days' => weekend_days}
end
After call Request.dates from hotels.rb you could access to all variables added to hash.
weekday_nm_tot = (Request.dates['weekdays'] * pricea.wdpricenm) + (Request.dates['weekdays'] * pricea.wepricenm)
I am having problem getting net-salary value. I have teacher_payslip model. For calculating net-salary,I have written callback.
In TeacherPayslip.rb
#callbacks
after_create :net_salary
def net_salary
#teacher_id = self.id
#da = (self.basic * self.da)/100
#hra = (self.basic * self.hra)/100
#gs = #da + #hra + self.basic
#pf = (#gs * self.pf)/100
#netsalary = #gs - #pf + self.special_allowance + self.bonus
#raise #netsalary.inspect
#a = TeacherPayslip.find(#teacher_id)
#raise #a.inspect
#a.update_attributes(:net_salary => #netsalary)
end
The net_salary value was updated in TeacherPayslip Model.
In Rails console, I have tried some code
TeacherPayslip.last.net_salary
Shows true value instead of net_salary value
I don't know, Why this query shows true value.. Please Help Me...
It's a naming collision. You're overwriting the method net_salary.
The return value of true is the return value of update_attributes.
To fix this rename your method and the callback to set_net_salary.
user it
TeacherPayslip.last.net_salary