On Oct 9, 10:24 am, Marnen Laibow-Koser <rails-mailing-l...@andreas-
s.net> wrote:
> MaggotChild wrote:
> > 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
>
> The easiest thing to do would be to modify the media table so as to
> declare the name to be primary key.  

Hi Maren,

  Modifying the DB isn't possible but I could use the :primary_key
options to has one:

class Segment < AR::Base
  belongs_to :show
  has_one :media, :primary_key => :media_name

  def media_name
    "#{name}%02d" % part
  end
end

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