I am creating a ‘match.com’ style app for local dog adoptions, where a user or a dog can create a profile. In each profile, there are boolean fields. I want to display the correct dog that has the same true value in the dog profile with the corresponding boolean field in the user profile. In the example, if Dog.kids_under_10 and User.profile.kids_under_10 match, the dog is displayed. I can accomplish this with a String or Integer (1 or 0), but I am trying to learn the best way to write code.
I will eventually be passing additional params, but I think once I understand how to make the below example functional, I should be able to simply add params as needed using the same logic.
Controller:
@user = current_user.profile
@dogs = Dog.by_profile(params[@user.kids_under_10])
Model:
def self.by_profile(user_profile)
Dog.where('kids_under_10 = ?', user_profile )
end
Your
@dogsassignment can look like thisThere is no need for your
by_profileclass method.If you want to keep the class method, your controller would look like this
and your model’s class method, like this