I'm attempting to wrestle an old DB into Rails.
This relationship is giving me trouble:

class Show < AR::Base
  has_many :segments
end

class Segment < AR::Base
  belongs_to :show
  has_one :media  #this has no PK/FK relation
end

A Segment is "linked" to Media by Media.name, which is the result of
concatenating Segment.name and Segment.part. As I said there are is no
PK/PK relation so the actual Segment class looks like this:

class Segment < AR::Base
  def media
    @media ||= Media.find_by_name("#{name}%02d" % part)
  end
end

This leaves me stuck with a  N+1 select problem because I can't say:
Segment.all :include => :media

or

Show.all :include => {:segment=>:media}

How to get around the N+1? select problem and eager load Media?

In the case of Show.all, Show and Media have a relation (yes bad
schema) so I was thinking I could pull all the
Media after a the Segment collection is loaded by Show and assign them
to the Segments accordingly.
But, while there is an after_find, this is applied after each object
and not after the collection.

Any ideas?

Thanks

--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to