Foreign_key setting ignored


#1

I’m having a spot of bother creating a ‘has_many’ relationship between
two legacy database tables. The relationship is ‘company’ table has many
linked records in the ‘contact’ table. Iv’e used ‘generate scaffold’ on
these tables and everything is fine. I then decided to define the
relationship in the relevant model classes…

class Contact < ActiveRecord::Base
belongs_to :company,:foreign_key => “companyid”
end

class Company < ActiveRecord::Base
has_many :contact
end

And then I’ve added the following line to views/company/show.rhtml to
try and get at the child elements…

<%= debug(@company.contact) %>

I’ve tried loads of variations on this setup, but always end up with an
error along the lines of…

|“invalid identifier: SELECT * FROM contact WHERE (contact.company_id = 100011847)”

|Rails appears to be ignoring the foreign_key setting in contact.rb and attempts to use company_id instead of companyid. Can anyone suggest what’s wrong with this setup?

Thanks in advance for your help,
Derek


#2

On 12/22/05, Derek M. removed_email_address@domain.invalid wrote:

class Company < ActiveRecord::Base

|“invalid identifier: SELECT * FROM contact WHERE (contact.company_id = 100011847)”

|Rails appears to be ignoring the foreign_key setting in contact.rb and attempts to use company_id instead of companyid. Can anyone suggest what’s wrong with this setup?

If your table is actually called “contact” instead of “contacts”,
you’ll also need to override that. ActiveRecord is looking for a file
with a different name than ‘contact.rb’, and not finding it.
So, try:
has_many :contact, :class_name => ‘Contact’

Alternately, you could rename the table and say:
has_many :contacts


#3

Derek M. wrote:

I’m having a spot of bother creating a ‘has_many’ relationship between
two legacy database tables. The relationship is ‘company’ table has many
linked records in the ‘contact’ table. Iv’e used ‘generate scaffold’ on
these tables and everything is fine. I then decided to define the
relationship in the relevant model classes…

I would be more verbose for those legacy tables:

class Contact < ActiveRecord::Base
set_table_name ‘contact’ # unless pluralize_table_names if off
belongs_to :company,:foreign_key => “companyid”
end

class Company < ActiveRecord::Base
set_table_name ‘company’
has_many :contacts, :class_name => ‘Contact’, :foreign_key =>
‘companyid’
end


#4

Lugovoi N. wrote:

Derek M. wrote:

I’m having a spot of bother creating a ‘has_many’ relationship between
two legacy database tables. The relationship is ‘company’ table has many
linked records in the ‘contact’ table. Iv’e used ‘generate scaffold’ on
these tables and everything is fine. I then decided to define the
relationship in the relevant model classes…

I would be more verbose for those legacy tables:

class Contact < ActiveRecord::Base
set_table_name ‘contact’ # unless pluralize_table_names if off
belongs_to :company,:foreign_key => “companyid”
end

class Company < ActiveRecord::Base
set_table_name ‘company’
has_many :contacts, :class_name => ‘Contact’, :foreign_key =>
‘companyid’
end

It’s now fixed, thanks for the help! If you’re interested…

contact.rb didn’t need changed and I’d already turned
pluralize_table_names off and added
ActiveRecord::Base.primary_key_prefix_type=:table_name
to environment.rb

I’d tried being verbose and setting the foreign_key in company.rb as
you’ve suggested, but it was the additional
:class_name => ‘Contact’
that fixed the has_many relationship for me :slight_smile: