Wednesday, October 13, 2010

Re: [Rails] Associations Pickle with Legacy Schema

That's because your association has the same name as the foreign key. Try:

belongs_to :my_account_manager, # Or any name other than 'account_manager'
  :class_name => 'User',
  :foreign_key => 'account_manager'

On Thu, Oct 14, 2010 at 8:58 AM, Greg Willits <lists@ruby-forum.com> wrote:
For the most part I'm working my through creating AR associations with a
legacy DB using the various options AR provides to identify tables and
fields.

My current snag is a little outside of that stuff.

I have a Company--with an account_manager attribute which is the ID of a
User. So..

class Company
belongs_to  :account_manager,
 :class_name =>'User',
 :foreign_key => "account_manager"

The User table has an autoinc ID, so that part works out.

This works fine for pulling the account_manager's name in a Company.find

But... when trying to save a new Company record, I get the following
error.

User(#blabla) expected, got String(#blabla)

OK. Expected what? And since I am saving a Company model, why is User
complaining?

account_manager is allowed to be NULL, and no data is being assigned to
the attribute prior to the save.

If I comment out the Company#belongs_to to eliminate the association,
then the save works fine.

I added

class User
has_many  :companies,
 :class_name => 'Company',
 :foreign_key => 'account_manager'

to reciprocate the association, but that makes no difference.

Googled, read my books, rails guides, but I'm stymied.

All suggestions appreciated. Thx.

-- gw

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.




--
Erol M. Fornoles

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate