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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
So I can achieve the desired behavior with the latest Arel:
ingredients = Ingredient.arel_table
Recipe.joins(:ingredients).where(ingredients[:name].matches_any("%chips
%", "%gravy%"))
Is this the cleanest way?
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Not sure how I deluded my self into believing that worked, but it didn''t. I still can''t figure out how to build this query. Either Google doesn''t want to be my friend or I''m asking the wrong question. Does anyone know of any blogs/tutorials that cover Rails 3 has_many :through queries? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.