Max Williams
2008-Dec-08 10:59 UTC
Association to return only the first of an associated list
A little background: in our schema, Resource has_many :assets, through a
join table called items.
Whenever we show a resource, we also show a thumbnail for its first
associated asset. Currently, this means that we''re doing a lot of sql
action in the view: for example, a partial like this might be repeated
100 times:
....
<% asset = resource.assets.first %>
<%= image_tag asset.name, asset.path %>
...
Obviously doing loads of little queries in the view isn''t good: what
i''d like to do is somehow eager-load that first asset with the include
option. We tried making a method called ''primary_asset'' which
returns
the first asset for a resource, but that can''t be "include"d:
i think
that in order to include it i need to set it up as an association.
Which leads me to my actual question: how do i set up a has_one
association to return only the first asset from the collection of
associated assets?
has_one doesn''t seem to have an ''sql'' option,
otherwise i''d just do
something like
has_one :primary_asset, :class_name => "Asset", :sql =>
"SELECT
assets.* FROM assets INNER JOIN items ON assets.id = items.asset_id
WHERE ((items.resource_id = #{id})) ORDER BY position LIMIT 1"
Can anyone see how i can set this up? I''m using rails 2.0.2.
thanks, max
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
JDevine
2008-Dec-08 18:19 UTC
Re: Association to return only the first of an associated list
Have you tried has_one :primary_asset, :class_name=>"Asset", :order=>"position" On Dec 8, 4:59 am, Max Williams <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> A little background: in our schema, Resource has_many :assets, through a > join table called items. > > Whenever we show a resource, we also show a thumbnail for its first > associated asset. Currently, this means that we''re doing a lot of sql > action in the view: for example, a partial like this might be repeated > 100 times: > > .... > <% asset = resource.assets.first %> > <%= image_tag asset.name, asset.path %> > ... > > Obviously doing loads of little queries in the view isn''t good: what > i''d like to do is somehow eager-load that first asset with the include > option. We tried making a method called ''primary_asset'' which returns > the first asset for a resource, but that can''t be "include"d: i think > that in order to include it i need to set it up as an association. > Which leads me to my actual question: how do i set up a has_one > association to return only the first asset from the collection of > associated assets? > > has_one doesn''t seem to have an ''sql'' option, otherwise i''d just do > something like > > has_one :primary_asset, :class_name => "Asset", :sql => "SELECT > assets.* FROM assets INNER JOIN items ON assets.id = items.asset_id > WHERE ((items.resource_id = #{id})) ORDER BY position LIMIT 1" > > Can anyone see how i can set this up? I''m using rails 2.0.2. > > thanks, max > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 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 -~----------~----~----~----~------~----~------~--~---