dcecuweb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2013-Mar-07 21:34 UTC
Filter via has_many, but show all has_many objects in result
I have a model, resource, which has_many categories:
has_many :category_resources, :dependent => :destroy
has_many :categories, :through => :category_resources
I have it setup to filter on categories, for a list view:
records =
Resource.includes(:categories).where("category_resources.category_id"
=>
categories)
But I want it to still show all categories for a particular resource the
listing, rather than just what was searched on.
eg, a resource, apple, has the categories, Food, Fruit, and Red.
If you select the resource Food, it should show:
Name: Apple
Categories: [Food, Fruit, Red]
Instead, I''m just getting:
Name: Apple
Categories: [Food]
What would be the best way to structure my query for this to work?
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/YRLQkXNFnlAJ.
For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Mar-08 08:59 UTC
Re: Filter via has_many, but show all has_many objects in result
On 7 March 2013 21:34, <dcecuweb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a model, resource, which has_many categories: > > has_many :category_resources, :dependent => :destroy > has_many :categories, :through => :category_resources > > I have it setup to filter on categories, for a list view: > records > Resource.includes(:categories).where("category_resources.category_id" => > categories)Can you explain in words what you are trying to achieve with the query above please.> > But I want it to still show all categories for a particular resource the > listing, rather than just what was searched on. > > eg, a resource, apple, has the categories, Food, Fruit, and Red. > > If you select the resource Food, it should show: > Name: Apple > Categories: [Food, Fruit, Red] > > Instead, I''m just getting: > Name: Apple > Categories: [Food]If you have a Resource in resource, then resource.categories should always show you all the categories. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2013-Mar-08 11:46 UTC
Re: Filter via has_many, but show all has_many objects in result
On Thursday, March 7, 2013 9:34:17 PM UTC, dcec...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > I have a model, resource, which has_many categories: > > has_many :category_resources, :dependent => :destroy > has_many :categories, :through => :category_resources > > I have it setup to filter on categories, for a list view: > records = > Resource.includes(:categories).where("category_resources.category_id" => > categories) > >This use of includes combined with the condition makes rails use a join to do the include, but, as you''ve found the filtering then filters the included rows, since everything is done with a single query. I''d try something like Resource.joins(:category_resources).where("category_resources.category_id" => categories).preload(:categories) assuming that you want the proloading for performance reasons. This splits out the eager loading stuff from the main query. Fred> But I want it to still show all categories for a particular resource the > listing, rather than just what was searched on. > > eg, a resource, apple, has the categories, Food, Fruit, and Red. > > If you select the resource Food, it should show: > Name: Apple > Categories: [Food, Fruit, Red] > > Instead, I''m just getting: > Name: Apple > Categories: [Food] > > What would be the best way to structure my query for this to work? >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Lnbom0OMNiMJ. For more options, visit https://groups.google.com/groups/opt_out.