Is there have any way can build one-to-many relationship without primary key

I have two models,Book and Picture,one book can have many pictures.
In database,I contact them by uuid column which is not a primary
key,roughly like this:

Books
Pictures
|--------------------------------- |-------------1:*(via
uuid)----------|------------------------------------|
|id | title | isbn |…|uuid
| |id|create_at|rating|…|

uuid

you can use :foreign_key like
has_many :pictures, ::foreign_key => “uuid”
or if that doesn’t work you could use the :finder_sql option
and define you own sql to find the related records.

uuid|
Yes, you can…

I’m doing some work with occupational data right now and everything
has an onet_soc_code which is not the primary key. It’s not
documented, but you can do this (primary_key tells the model to use
that column as the local side for looking things up instead of the
tables real primary key).

Occupation:
has_many :aliases,
:class_name => ‘OccupationAlias’,
:primary_key => :onet_soc_code,
:foreign_key => :onet_soc_code,
:order => ‘title’

Going the other way requires a patch to Rails, but lets you do this:

OccupationAlias:
belongs_to :occupation,
:primary_key => :onet_soc_code,
:foreign_key => :onet_soc_code

The patch is here:

http://rails.lighthouseapp.com/projects/8994/tickets/765-primary_key-option-for-belongs_to

-philip

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs