Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Setting an addional attribute on the join model in a has_many through relationship

Received: by 10.42.110.75 with SMTP id o11mr13933553icp.6.1335904191806;
        Tue, 01 May 2012 13:29:51 -0700 (PDT)
X-BeenThere: rubyonrails-talk@googlegroups.com
Received: by 10.231.83.209 with SMTP id g17ls4480466ibl.2.gmail; Tue, 01 May
 2012 13:29:02 -0700 (PDT)
Received: by 10.43.53.73 with SMTP id vp9mr20173202icb.0.1335904142816;
        Tue, 01 May 2012 13:29:02 -0700 (PDT)
Received: by 10.43.53.73 with SMTP id vp9mr20173193icb.0.1335904142786;
        Tue, 01 May 2012 13:29:02 -0700 (PDT)
Return-Path: <clan...@googlemail.com>
Received: from mail-ob0-f175.google.com (mail-ob0-f175.google.com [209.85.214.175])
        by gmr-mx.google.com with ESMTPS id md3si8597291igc.1.2012.05.01.13.29.02
        (version=TLSv1/SSLv3 cipher=OTHER);
        Tue, 01 May 2012 13:29:02 -0700 (PDT)
Received-SPF: pass (google.com: domain of clan...@googlemail.com designates 209.85.214.175 as permitted sender) client-ip=209.85.214.175;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of clan...@googlemail.com designates 209.85.214.175 as permitted sender) smtp.mail=clan...@googlemail.com; dkim=pass header...@googlemail.com
Received: by obhx4 with SMTP id x4so5533547obh.20
        for <rubyonrails-talk@googlegroups.com>; Tue, 01 May 2012 13:29:02 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=googlemail.com; s=20120113;
        h=mime-version:in-reply-to:references:from:date:message-id:subject:to
         :content-type:content-transfer-encoding;
        bh=rgXRJFYuvvTpuGVIH2KqvQyxh0WAvi4G8hqylpE44FE=;
        b=tmQyNJwZEUiFu5uo+2v/Gcf0Qr+SFMaKCyyV3e5UtVQr6XwV66fFRNlCOLEJ2gCTf7
         IJI2oK42CMO7f3zUire/QBQw2ct/n3u6TdzNvVVmH7EU894ao8W9alqPS88kY98og9H/
         2VEGA90+4rgsudlPJCJ+fBwv7UbeGwGiQLUF0E5z7uu+R7TuwM4SLismy/cxrxM/SjkK
         HXtxRFUAnRtoziNvOtz7LDJS4JuvJSehLWJgDBCfSX9ZBvWMdWijE4I4lU1zlUJVAU94
         aCHjJzLnjLABmp4A61lcqwtuWUes1WSiR8SiuEa8tWPm4cUFocIeK/I8ZgEZxYNwUduc
         82ZQ==
Received: by 10.182.183.73 with SMTP id ek9mr10695729obc.15.1335904141675;
 Tue, 01 May 2012 13:29:01 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.182.165.105 with HTTP; Tue, 1 May 2012 13:28:31 -0700 (PDT)
In-Reply-To: <15054935.1485.1335888305971.JavaMail.geo-discussion-forums@yncd9>
References: <15054935.1485.1335888305971.JavaMail.geo-discussion-forums@yncd9>
From: Colin Law <clan...@googlemail.com>
Date: Tue, 1 May 2012 21:28:31 +0100
Message-ID: <CAL=0gLvzLTx+FjcQd=WyvADDPhB=v=OiY0DhT_VEDft15GZ...@mail.gmail.com>
Subject: Re: [Rails] Setting an addional attribute on the join model in a
 has_many through relationship
To: rubyonrails-talk@googlegroups.com
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On 1 May 2012 17:05, Mohamad El-Husseini <husseini....@gmail.com> wrote:
> I have User, Account, and Role models. The Account model accepts nested
> properties for users. This way users can create their account and user
> records at the same time.
>
> class AccountsController < ApplicationController
> =C2=A0 def new
> =C2=A0 =C2=A0 @account =3D Account.new
> =C2=A0 =C2=A0 @user =3D @account.users.build
> =C2=A0 end
> end
>
> The above will work, but the=C2=A0user.roles.type=C2=A0defaults to=C2=A0m=
ember. At the time
> of registration, I needuser.roles.type=C2=A0to default to=C2=A0admin. Thi=
s does not
> work:
>
> class AccountsController < ApplicationController
> =C2=A0 def new
> =C2=A0 =C2=A0 @account =3D Account.new
> =C2=A0 =C2=A0 @role =3D @account.role.build
> =C2=A0 =C2=A0 # Role.type is protected; assign manually
> =C2=A0 =C2=A0 @role.type =3D "admin"
> =C2=A0 =C2=A0 @user =3D @account.users.build
> =C2=A0 end
> end

It depends what you mean by 'work'.  It will assign the type of @role
to "admin" but the problem is that you have not saved it to the
database after changing the type.  By the way, I advise against using
type as an attribute name, that is a reserved attribute name for use
with STI.

> ...

> # user_id, account_id, type [admin|moderator|member]
> class Role < ActiveRecord::Base
> =C2=A0 belongs_to :user
> =C2=A0 belongs_to :account
> =C2=A0 after_initialize :init
>
> =C2=A0 ROLES =3D %w[owner admin moderator member]
>
> =C2=A0 private
> =C2=A0 def init
> =C2=A0 =C2=A0 self.role =3D "member" if self.new_record?
> =C2=A0 end
> end

Should that not be self.type (apart from the fact that type is not a
good name)?  But if you want a default value for a column why not just
set the default in the database?

Colin