I am working with an existing database and have to keep all table and field names as they are. ''tblcustomers'' and ''tblitems'' are linked by the ''tblcustomers'' id field ''tblcustomersid''. In ''tblitems'', the foreign key is called ''txtcustomerid''. I have already set the customers model to override the ''id'' naming convention for the primary key by adding the following to the customers model. set_primary_key(:intcustomerid) How do I tell rails to override the preferred name for the foreign key ''txtcustomerid''? I have tried: set_foreign_key(:intcustomerid) and :foreign_key => "intcustomerid" but neither work. I''m running out of ideas! Thanks, Alana -- Posted via http://www.ruby-forum.com/.
Hi, Can anyone help with this problem? Alana -- Posted via http://www.ruby-forum.com/.
Alana wrote:> Hi, > > Can anyone help with this problem? > > AlanaYou define custom foreign key when you define the relationship between the tables in your model file.>From page 231 of Agile Web Development With Rails:class LineItem < ActiveRecord::Base belongs_to :paid_order, :class_name => "Order, :foreign_key => "order_id", :conditions => "paid_on is not null" end so in your example you might say: class Item < ActiveRecord::Base set_table_name "tblitems" belongs_to :customer, :foreign_key => "txtcustomersid" end and class Customer < ActiveRecord::Base set_table_name "tblcustomers" belongs_to :items, :foreign_key => "tblcustomerid" end Something like that. Jeff -- Posted via http://www.ruby-forum.com/.
Try here: http://wiki.rubyonrails.com/rails/pages/HowToUseLegacySchemas Alana wrote:> I am working with an existing database and have to keep all table and > field names as they are. ''tblcustomers'' and ''tblitems'' are linked by the > ''tblcustomers'' id field ''tblcustomersid''. In ''tblitems'', the foreign key > is called ''txtcustomerid''. > > I have already set the customers model to override the ''id'' naming > convention for the primary key by adding the following to the customers > model. > > set_primary_key(:intcustomerid) > > How do I tell rails to override the preferred name for the foreign key > ''txtcustomerid''? I have tried: > > set_foreign_key(:intcustomerid) and :foreign_key => "intcustomerid" > > but neither work. I''m running out of ideas! > > Thanks, > Alana-- Posted via http://www.ruby-forum.com/.
Hi Jeff, Thanks for your reply. How can the class be "Customer" when the table is called "tblcustomers"? should the class not be set to "Tblcustomers"?> class Customer < ActiveRecord::Base > set_table_name "tblcustomers" > belongs_to :items, > :foreign_key => "tblcustomerid" > endThanks, Alana -- Posted via http://www.ruby-forum.com/.
barthelemy von Haller
2006-Apr-10 14:33 UTC
[Rails] Re: Foreign Key naming convention override
Alana wrote:> Hi Jeff, > > Thanks for your reply. How can the class be "Customer" when the table is > called "tblcustomers"? should the class not be set to "Tblcustomers"? > >> class Customer < ActiveRecord::Base >> set_table_name "tblcustomers" >> belongs_to :items, >> :foreign_key => "tblcustomerid" >> end >The code above says that the class Customer is mapped to the table ''tblcustomers'' thanks to the set_table_name method, no ? regards, Barth -- Posted via http://www.ruby-forum.com/.
Alana wrote:> Hi Jeff, > > Thanks for your reply. How can the class be "Customer" when the table is > called "tblcustomers"? should the class not be set to "Tblcustomers"? > >> class Customer < ActiveRecord::Base >> set_table_name "tblcustomers" >> belongs_to :items, >> :foreign_key => "tblcustomerid" >> end > > Thanks, > AlanaThe "set_table_name" property lets you work with a legacy schema but still have readable code and class names. Oh, and I made a mistake in the above code. I believe what you''re describing for Customer and Item is a has_many relationship, where Customer has_many Items... class Customer < ActiveRecord::Base set_table_name "tblcustomers" has_many :items, :foreign_key => "tblcustomerid" end Jeff Coleman -- Posted via http://www.ruby-forum.com/.