Re: Re: Rails HABTM records

Colin Law
Wed, 06 Apr 2011 16:07:51 -0000

On 6 April 2011 16:36, Sachin Joshi
<sachin.joshi-Pif7/TdT6AJWk0Htik3J/w@public.gmane.org>
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

>    -Pfb+/kRXlPON7sh4/YkoqQ@public.gmane.org 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

>    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..