Forum: Ruby on Rails foreign_key setting ignored

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.
Fd00fdfc23ab2cea4ae7a5b429d590ee?d=identicon&s=25 Derek Mailer (Guest)
on 2005-12-22 17:17
(Received via mailing list)
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
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2005-12-22 17:23
(Received via mailing list)
On 12/22/05, Derek Mailer <derek.mailer@iomart.com> 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
5c19f2d52879a1e10670c7334ba4c7e3?d=identicon&s=25 Lugovoi Nikolai (Guest)
on 2005-12-22 17:29
(Received via mailing list)
Derek Mailer 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
Fd00fdfc23ab2cea4ae7a5b429d590ee?d=identicon&s=25 Derek Mailer (Guest)
on 2006-01-03 16:17
Lugovoi Nikolai wrote:
> Derek Mailer 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 :)
This topic is locked and can not be replied to.