On 15 January 2012 19:33, Peter Vandenabeele <pe...@vandenabeele.com> wrote:
> On Sun, Jan 15, 2012 at 7:25 PM, Mauro <mrsan...@gmail.com> wrote:
>>
>> On 15 January 2012 17:54, Peter Vandenabeele <pe...@vandenabeele.com>
>> wrote:
>>
>> >> I want to assign a role to Management at the same time I create a new
>> >> Manager.
>> >
>> >
>> > I understand, but I was unable to find a solution that does it in 1
>> > time...
>>
>> And tha'ts my problem, reading other posts in internet it seems
>> impossible.
>> Perhaps the role can be set with a before_save in Management model but
>> how to use in Management model the params of the manager form?
>
>
> I would not use before_filter for this.
>
> Just do it in 2 separate saves.
>
> 1) build the manager indepedently
> 2) build the management with all associations and his role
>
> Save them. You might be lucky to do @manager.save that
> saves both of them if you had built the management with
> the build on the association from @manager. Like this:
>
> Pseudo code (untested)
>
> @company = Company.find(params[:company_id]) # 1 FIND
>
> @manager = Manager.new(params[:manager])
> @manager.managements.build(:role => role, :company => @company)

I've tried something like this but the result in the managements table is:

company_id => 1, manager_id => 1, role => nil
company_id => nil, manager_id =>1, role => "test"

It creates two records while I want:

company_id => 1, manager_id => 1, role => "test"

Is it really hard to do this with rails?

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to