I really don't know hot to set an extra attribute of a join model

This seems to work:

def new
@company = Company.find(params[:company_id])
@manager = Manager.new
@manager.managements.build
new!
end

def create
@company = Company.find(params[:company_id])
@manager = Manager.new(params[:manager])
@manager.managements.build(:role=>params[:role],:company => @company)
create!
end

Am I correct now?

On 15 January 2012 22:11, Peter V. [email protected]
wrote:

Possibly, for company and manager.
“commit”=>“Crea Amministratore”, “company_id”=>“6”}

I did not see a

“role” => “Something”

That’s because I didn’t have @manager.managements.build in the
controller.

On Sun, Jan 15, 2012 at 10:15 PM, Mauro [email protected] wrote:

new!

Am I correct now?
“manager”=>{“name”=>“q”, “surname”=>“”,
“fiscal_code”=>“1121111111111111”, “city”=>“”, “zip_code”=>“”,
“address”=>“”, “street_number”=>“”, “tel”=>“”, “email”=>“”},
“commit”=>“Crea Amministratore”, “company_id”=>“6”}

I did not see a

“role” => “Something”

That’s because I didn’t have @manager.managements.build in the controller.

I doubt it …

What you get back in “params” when the “create” action is called,
is a function of the code and view that is rendered by the “new” action.

Changing the code in the “create” action cannot change the params
you receive when that “create” action is called.

Unless I totally misunderstood the way you would use “new” and “create”

Anyway, does it work now, does that mean the params when entering the
“create” action now do have the correct “role” key in them, than it is
fine
:slight_smile:

HTH,

Peter

On Sun, Jan 15, 2012 at 10:21 PM, Peter V.
<[email protected]

wrote:

@manager = Manager.new

end
“authenticity_token”=>"
That’s because I didn’t have @manager.managements.build in the controller.
Unless I totally misunderstood the way you would use “new” and “create” …

Which is quite possible if Javascript is involved …
I was only thinking of the simple classic case of
index => new => create => show

Peter

On Sun, Jan 15, 2012 at 10:25 PM, Mauro [email protected] wrote:

def new
@company)

“role” => “Something”

In the new action I didn’t have @manager.managements.build so the form
couldn’t create the list of the roles to assign to management.role

Ah I see now, the code you refer to is in the “new” action as well
(sorry,
I did not check careful enough). And of course, there it can have that
effect.

And sorry to the list for the large volume of posts about this case.

Peter

And sorry to the list for the large volume of posts about this case.

No no it’s my ignorance, you have helped me a lot, I’ve solved a
problem that I had for months.
Really thanks a lot.

On 15 January 2012 22:21, Peter V. [email protected]
wrote:

@manager = Manager.new

end
“authenticity_token”=>"
That’s because I didn’t have @manager.managements.build in the controller.

I doubt it …

What you get back in “params” when the “create” action is called,
is a function of the code and view that is rendered by the “new” action.

In the new action I didn’t have @manager.managements.build so the form
couldn’t create the list of the roles to assign to management.role

Categorization has two extra attributes, :classification_id and :amount.
Now I want to assign a value to classification_type when I create a new company.

I mean classification_id and not classification_type.

Can I ask some help for another similar problem?
The first problem we have solved with your great help was:

class Company < ActiveRecord::Base
has_many :managements
has_many :managers, :through => :managements

class Manager < ActiveRecord::Base
has_many :managements
has_many :companies, :through => :managements

Management has an extra attribute :role and to assign a value to role
when we create a new company.manager we have set in the manager
controller:

def create
@company = Company.find(params[:company_id])
@manager = Manager.new(params[:manager])
@manager.managements.build(:role => params[:manager][:role],
:company => @company)

It works!!.

Now I’ve extended Company model and it is.

class Company < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through => :categorizations
has_many :managements
has_many :managers, :through => :managements

class Category < ActiveRecord::Base
has_many :categorizations
has_many :companies, :through => :categorizations

Categorization has two extra attributes, :classification_id and :amount.
Now I want to assign a value to classification_type when I create a new
company.
In the controller I’ve done:

def create
@company = Company.new(params[:company])
@company.categorizations.build(:classification_id =>
params[:company][:classification])

but it creates two categorizations:

[#<Categorization id: 111, company_id: 106, category_id: nil,
classification_id: 2, amount: nil, created_at: “2012-01-16 20:12:18”,
updated_at: “2012-01-16 20:12:18”>, #<Categorization id: 112,
company_id: 106, category_id: 1, classification_id: nil, amount: nil,
created_at: “2012-01-16 20:12:18”, updated_at: “2012-01-16
20:12:18”>].