Associations Pickle with Legacy Schema

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

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 W. [email protected]
wrote:

belongs_to :account_manager,
User(#blabla) expected, got String(#blabla)
I added
All suggestions appreciated. Thx.
To unsubscribe from this group, send email to

[email protected][email protected]

.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Erol M. Fornoles
http://github.com/Erol
http://twitter.com/erolfornoles
http://ph.linkedin.com/in/erolfornoles

Well, sheeeoooot. Many thanks.

– gw

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs