Using attr_accessible in a join model with has_many :through relationship
Posted
by
Paulo Oliveira
on Stack Overflow
See other posts from Stack Overflow
or by Paulo Oliveira
Published on 2011-06-22T03:00:14Z
Indexed on
2012/03/28
23:31 UTC
Read the original article
Hit count: 312
I have a USER that creates a COMPANY and become an EMPLOYEE in the process. The employees table has an :user_id
and a :company_id
.
class User
has_many :employees
has_many :companies, :through => :employees
class Employee
belongs_to :user
belongs_to :company
attr_accessible :active
class Company
has_many :employees
has_many :users, :through => employees
Pretty basic. But here's the thing, the resource EMPLOYEE has other attributes than its foreign keys, like the boolean :active
. I would like to use attr_accessible
, but this causes some problems. The attribute :user_id
is set right, but :company_id
is nil.
@user.companies << Company.new(...)
Employee id:1 user_id:1 company_id:nil
So my question is: if :user_id
is set right, despite it is not an attr_accessible
, why :company_id
isn't set right just the same? It shouldn't be an attr_accessible
.
I'm using Rails 3.0.8, and have also tested with 3.0.7.
© Stack Overflow or respective owner