I am trying to pass a simple variable in my params to a class method, however it doesnt seem to work. This seems elementary, but I’m still learning. Can someone explain why this doesn’t work and offer an alternative? My code is below.
controller
@profile = current_user.profile
@dogs = Dog.by_profile(params[@profile])
model
def self.by_profile(profile)
Dog.where(kids_under_10: profile.kids_under_10 )
end
*note: profile.kids_under_10 is a boolean. When I manually replace it with true or false, everything works fine.
paramsis a special rails hash that contains url parameters. So your code is looking for a url parameter passed with the request containing the string version of your user profile. This is definitely not what you want to be doing.When you’re calling a rails model method, you call it with arguments like any other method:
Dog.by_profile(@profile)You don’t want the params part, or you’re trying to do something crazy that should be refactored 🙂