John Jiang
John Jiang

Reputation: 282

conflict between attribute name and foreign key name

I am wondering if it is possible to have the following situation: I have a person object that has both an attribute called email and a foreign key called email which is realized as a has_many relationship. The latter is supposed to contain not only the primary email but also secondary ones. Would this cause conflict when I invoke @person.email? What would be the standard way to work around this? Thanks.

Upvotes: 0

Views: 115

Answers (1)

davidb
davidb

Reputation: 8954

Create a secound field that could be used as a foreign key and then in the associated model add :foreign_key => 'your_foreign_key' as aparameter to the existing association like this:

has_many :email_addresses, :class_name => 'ClassName', :foreign_key => 'your_foreign_key'

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html <-- Documentation about that.

Upvotes: 1

Related Questions