On 6 April 2011 04:56, Sachin J. [email protected] wrote:
I have a HABTM association between User and Account model through a
I am surprised it works, I would have expected Rails to look for table
accounts_users, unless you are specifying has_many through, rather
the association seems to be working fine, e.g.
when i do
That should be @user.accounts. If @user.account works then you have
not specified the relationships correctly.
it fetches the associated record which is just one record as of now.
=> [#<Account id: 1, acc_name: “test account”, created_at: “2011-03-16
10:39:03”, updated_at: “2011-04-03 01:58:03”>]
Notice the square brackets. This is an array of accounts, containing
When i use @user.account.acc_name in the view, it throws the following
undefined method `acc_name’ for #Class:0x317e6b0
That is because @user.account is an array. You need something like
@user.account.acc_name, or @user.account.first.acc_name.
If you still can’t work it out post the class definitions with the
association specifications (copy and paste from your code so as to
avoid confusing typos).