Rails HABTM records

Hello Experts,
I have a HABTM association between User and Account model through a
table users_accounts
the association seems to be working fine, e.g.
when i do

@user.account
it fetches the associated record which is just one record as of now.
@user.account
=> [#<Account id: 1, acc_name: “test account”, created_at: “2011-03-16
10:39:03”, updated_at: “2011-04-03 01:58:03”>]

When i use @user.account.acc_name in the view, it throws the following
error.

undefined method `acc_name’ for #Class:0x317e6b0

I am using rails 3.0.xx any help appreciated.

Thanks in Advance
Sachin

On 6 April 2011 04:56, Sachin J. [email protected] wrote:

Hello Experts,
I have a HABTM association between User and Account model through a
table users_accounts

I am surprised it works, I would have expected Rails to look for table
accounts_users, unless you are specifying has_many through, rather
than HABTM.

the association seems to be working fine, e.g.
when i do

@user.account

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.
@user.account
=> [#<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
one element.

When i use @user.account.acc_name in the view, it throws the following
error.

undefined method `acc_name’ for #Class:0x317e6b0

That is because @user.account is an array. You need something like
@user.account[0].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).

Colin

Hi Colin,
Thanks for the pointers, in the mean time i got a work around on the
lines you mentioned.
Since it is HABTM relationship it is fetching array of accounts.
e.g.

def index
@user = User.find(current_user)
@account = current_user.account
@account.each do |a|
@ac_id = a.id
end
end

And i am able to use the @ac_id, but i am sure this not the clever way
to do this.
In the current specs i have 1 admin_user for 1 one account and this
account can have multiple users.

but this HABTM provision is made for future in which any of the sub
user of an existing account should be able to extend it to a owner
account.
I shall post any update…

Thanks,
Sachin

On 6 April 2011 16:36, Sachin J. [email protected] wrote:

Hi Colin,
Thanks for the pointers, in the mean time i got a work around on the
lines you mentioned.
Since it is HABTM relationship it is fetching array of accounts.
e.g.

def index
@user = User.find(current_user)

Is current_user a User object? If so just do @user = current_user

@account = current_user.account

If you know (at the moment) that there is only one account then do
@account = current_user.account.first
If you are allowing for multiple accounts then call it @accounts not
@account so that you will remember there may be several, or at least
for the moment it is an array of 1 element

@account.each do |a|
@ac_id = a.id

Why do you need the id? Sometimes you do need to access the id
directly, but mostly it is not necessary.
Also remember that if the user has not got an account then
current_user.accounts will be an empty array so in your code above
@ac_id would not be assigned. You may have to allow for this in the
view (or wherever you try to use @ac_id).

By the way could you not top post please, it makes it difficult to
follow the thread. Insert you reply at appropriate points in the
previous message. Thanks.

Colin

Thanks Colin,
Thanks for the precise useful pointers, i will let you know the
outcome.

Cheers,
Sachin

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