Forum: Ruby on Rails Foreign Key naming convention override

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
434eecf2bd6df521bfd599e403be9996?d=identicon&s=25 Alana (Guest)
on 2006-04-07 18:18
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
434eecf2bd6df521bfd599e403be9996?d=identicon&s=25 Alana (Guest)
on 2006-04-10 10:27
Hi,

Can anyone help with this problem?

Alana
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-10 10:39
Alana wrote:
> Hi,
>
> Can anyone help with this problem?
>
> Alana

You 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
B39d86f427c04b5df1e8ceca920bd14d?d=identicon&s=25 Kasper Weibel (Guest)
on 2006-04-10 10:42
Try here:
http://wiki.rubyonrails.com/rails/pages/HowToUseLe...

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
434eecf2bd6df521bfd599e403be9996?d=identicon&s=25 Alana (Guest)
on 2006-04-10 12:57
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,
Alana
F1a132bd8360c35d1c6f69dfed11f194?d=identicon&s=25 Barthelemy Von haller (barthelemy)
on 2006-04-10 16:33
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
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-10 19:04
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,
> Alana

The "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
This topic is locked and can not be replied to.