# Rails HABTM records

**URL:** <https://www.ruby-forum.com/t/rails-habtm-records/205424>\
**Category:** Rails\
**Created:** [April 6, 2011, 11:13am UTC](https://www.ruby-forum.com/t/rails-habtm-records/205424 "2011-04-06T11:13:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 6, 2011, 11:13am UTC](https://www.ruby-forum.com/t/rails-habtm-records/205424/1 "2011-04-06T11:13:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sachinjoshi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@sachinjoshi](https://www.ruby-forum.com/u/sachinjoshi)\
**Post date:** [April 6, 2011, 11:58am UTC](https://www.ruby-forum.com/t/rails-habtm-records/205424/2 "2011-04-06T11:58:58Z")

</div>

On 6 April 2011 04:56, Sachin J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![sachinjoshi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@sachinjoshi](https://www.ruby-forum.com/u/sachinjoshi)\
**Post date:** [April 6, 2011, 5:38pm UTC](https://www.ruby-forum.com/t/rails-habtm-records/205424/3 "2011-04-06T17:38:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sachinjoshi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@sachinjoshi](https://www.ruby-forum.com/u/sachinjoshi)\
**Post date:** [April 6, 2011, 6:09pm UTC](https://www.ruby-forum.com/t/rails-habtm-records/205424/4 "2011-04-06T18:09:42Z")

</div>

On 6 April 2011 16:36, Sachin J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![sachinjoshi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@sachinjoshi](https://www.ruby-forum.com/u/sachinjoshi)\
**Post date:** [April 7, 2011, 6:17am UTC](https://www.ruby-forum.com/t/rails-habtm-records/205424/5 "2011-04-07T06:17:33Z")

</div>

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

Cheers,  
Sachin
