From: Brian Candler Date: 2010-06-08T23:22:57+09:00 Subject: Re: Looking for ORM for 'legacy' database. Brian Candler wrote: > class Playlist < ActiveRecord::Base > self.table_name = 'venue_playlist' > self.primary_key = 'plt_id' > > # Similarly for foreign key relations > has_many :playlist_items, :foreign_key => 'pli_playlist_id', > :order => 'pli_order', :dependent => :delete_all > end And for reference, here's the relation from the other side: class PlaylistItem < ActiveRecord::Base self.table_name = 'playlist_item' self.primary_key = 'nonexistent' belongs_to :playlist, :foreign_key=>'pli_playlist_id' end Notice that this particular table didn't really have a primary key as such; each row was a unique tuple. Setting the primary_key to 'nonexistent' just proves that AR never attempted to use it. At the controller side, all I needed to do was to set the attributes I was interested in when creating the object. def add_item ... PlaylistItem.create!( :pli_playlist_id => @playlist.plt_id, :pli_track_id => ... :pli_order => ... ) ... end -- Posted via http://www.ruby-forum.com/.