This is somewhat of an updated question from a problem I had earlier. I am creating a ‘match.com’ style app for local dog adoptions, where users and dogs have a profile. In each profile, there are Boolean fields. I am trying to show the end user only the dogs that match their user profile. A simple one-to-one match does not work however since the logic changes depending on the user’s Boolean. For example, if a user has kids (user.kids_under_10: true) then I simply match it to a dog that can be placed with kids (dog.kids_under_10:true). The model would look like
class Dog < ActiveRecord::Base
def self.by_profile(user)
if user.kids_under_10 == true
Dog.where(kids_under_10: user.kids_under_10)
end
end
If the user does not have kids however, dogs that answer both true and false to this question can be displayed, since dogs that are AND are not kid friendly can be placed with this user. I realize that if this were the only criteria for matching, I could simply add a else, Dog.all statement to the above method. Since there will other Boolean fields to match however (i.e. user.has_cats and dog.cat_friendly), this solution would not work. I think I need a variable containing an array here, but not really sure… any suggestions.
So I found a solution, however I ‘m not sure it’s the most efficient solution, so any suggestions are very welcome (I am new to programming and looking to learn the ‘best’ way to handle different situations). Here is how I solved the issue:
I can foresee this solution becoming a bit cumbersome as I add more parameters. The conditional statements could get quite long. Thanks for any suggestions!