Saturday, June 5, 2010

[Rails] Rails 3 queries

I have the following models:

class Recipe < ActiveRecord::Base
has_many :ingredient_amounts
has_many :ingredients, :through => :ingredient_amounts
end

class Ingredient < ActiveRecord::Base
def self.named_like(name)
where('ingredients.name ILIKE ?', "%#{name}%")
end
end

How do I query for all the recipes that include the ingredients
"chips" and "gravy".

I've tried:

Recipe.joins(:ingredients) & Ingredient.named_like("chips") &
Ingredient.named_like("gravy")

but that gives me recipes that include ingredients with names that
include both "chips" and "gravy" (e.g. named "gravy chips" or "spicy
chips with gravy", etc)

Thanks in advance,
Bryan

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate