Hello.
I need an advice to find a solution on creating a new has_many
:through association on a existing model.
I have:
Company
has_many :managements, :dependent => :destroy
has_many :managers, :through => :managements
Manager
has_many :managements, :dependent => :destroy
has_many :companies, :through => :managements
With the command
link_to "Add manager", new_company_manager_url(@company), :class =>
"btn", :remote => true
I display the manager form, create a new manager associated to the
company and create a new management.
In manager controller:
def create
@company = Company.find(params[:company_id])
@manager = Manager.new(params[:manager])
add_management
@manager.save
respond_with @manager
end
private
def add_management
profile = params[:manager][:profile]
profile.delete_if { |v| v.empty? }
profile.each do |prof|
@manager.managements.build(:profile => prof, :company => @company)
end
end
If the manager exists I only need to create a new management, so in
the manager new action I''ve put:
def new
@company = Company.find(params[:company_id])
@manager = Manager.find_or_initialize_by_fiscal_code(params[:fiscal_code])
respond_with(@manager)
end
If the manager does not exists I create a new one and a new management
like showed before.
But, if the manager exists the form action is to update manager.
I''m searching for a solution to only create a new management without
updating manager.
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On 31 May 2012 09:40, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello. > I need an advice to find a solution on creating a new has_many > :through association on a existing model. > I have: > > Company > has_many :managements, :dependent => :destroy > has_many :managers, :through => :managements > > Manager > has_many :managements, :dependent => :destroy > has_many :companies, :through => :managements > > With the command > link_to "Add manager", new_company_manager_url(@company), :class => > "btn", :remote => true > > I display the manager form, create a new manager associated to the > company and create a new management. > In manager controller: > > def create > @company = Company.find(params[:company_id]) > @manager = Manager.new(params[:manager]) > add_management > -NPpDW1XWqP5LYScDn9snng@public.gmane.org > respond_with @manager > end > > private > def add_management > profile = params[:manager][:profile] > profile.delete_if { |v| v.empty? } > profile.each do |prof| > -NPpDW1XWqP7zboGJfjnu6uyjaAAtfbygYKB5T7WKXak@public.gmane.org(:profile => prof, :company => @company) > end > end > > If the manager exists I only need to create a new management, so in > the manager new action I''ve put: > > def new > @company = Company.find(params[:company_id]) > @manager = Manager.find_or_initialize_by_fiscal_code(params[:fiscal_code]) > respond_with(@manager) > end > > If the manager does not exists I create a new one and a new management > like showed before. > But, if the manager exists the form action is to update manager. > I''m searching for a solution to only create a new management without > updating manager.Why can''t you do what you want in the update action? If the details of the manager record have not changed then the update will do nothing. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 31 May 2012 12:38, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 31 May 2012 09:40, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hello. >> I need an advice to find a solution on creating a new has_many >> :through association on a existing model. >> I have: >> >> Company >> has_many :managements, :dependent => :destroy >> has_many :managers, :through => :managements >> >> Manager >> has_many :managements, :dependent => :destroy >> has_many :companies, :through => :managements >> >> With the command >> link_to "Add manager", new_company_manager_url(@company), :class => >> "btn", :remote => true >> >> I display the manager form, create a new manager associated to the >> company and create a new management. >> In manager controller: >> >> def create >> @company = Company.find(params[:company_id]) >> @manager = Manager.new(params[:manager]) >> add_management >> -NPpDW1XWqP5LYScDn9snng@public.gmane.org >> respond_with @manager >> end >> >> private >> def add_management >> profile = params[:manager][:profile] >> profile.delete_if { |v| v.empty? } >> profile.each do |prof| >> -NPpDW1XWqP7zboGJfjnu6uyjaAAtfbygYKB5T7WKXak@public.gmane.org(:profile => prof, :company => @company) >> end >> end >> >> If the manager exists I only need to create a new management, so in >> the manager new action I''ve put: >> >> def new >> @company = Company.find(params[:company_id]) >> @manager = Manager.find_or_initialize_by_fiscal_code(params[:fiscal_code]) >> respond_with(@manager) >> end >> >> If the manager does not exists I create a new one and a new management >> like showed before. >> But, if the manager exists the form action is to update manager. >> I''m searching for a solution to only create a new management without >> updating manager. > > Why can''t you do what you want in the update action? If the details > of the manager record have not changed then the update will do > nothing.Uhm it sounds strange to me creating a new management association in the manager update action. I mean update only to to update manger attributes. Perhaps I''m wrong. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 31 May 2012 11:53, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 31 May 2012 12:38, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 31 May 2012 09:40, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> Hello. >>> I need an advice to find a solution on creating a new has_many >>> :through association on a existing model. >>> I have: >>> >>> Company >>> has_many :managements, :dependent => :destroy >>> has_many :managers, :through => :managements >>> >>> Manager >>> has_many :managements, :dependent => :destroy >>> has_many :companies, :through => :managements >>> >>> With the command >>> link_to "Add manager", new_company_manager_url(@company), :class => >>> "btn", :remote => true >>> >>> I display the manager form, create a new manager associated to the >>> company and create a new management. >>> In manager controller: >>> >>> def create >>> @company = Company.find(params[:company_id]) >>> @manager = Manager.new(params[:manager]) >>> add_management >>> -NPpDW1XWqP5LYScDn9snng@public.gmane.org >>> respond_with @manager >>> end >>> >>> private >>> def add_management >>> profile = params[:manager][:profile] >>> profile.delete_if { |v| v.empty? } >>> profile.each do |prof| >>> -NPpDW1XWqP7zboGJfjnu6uyjaAAtfbygYKB5T7WKXak@public.gmane.org(:profile => prof, :company => @company) >>> end >>> end >>> >>> If the manager exists I only need to create a new management, so in >>> the manager new action I''ve put: >>> >>> def new >>> @company = Company.find(params[:company_id]) >>> @manager = Manager.find_or_initialize_by_fiscal_code(params[:fiscal_code]) >>> respond_with(@manager) >>> end >>> >>> If the manager does not exists I create a new one and a new management >>> like showed before. >>> But, if the manager exists the form action is to update manager. >>> I''m searching for a solution to only create a new management without >>> updating manager. >> >> Why can''t you do what you want in the update action? If the details >> of the manager record have not changed then the update will do >> nothing. > > Uhm it sounds strange to me creating a new management association in > the manager update action. > I mean update only to to update manger attributes. > Perhaps I''m wrong.If you want to do it in the manager controller then update is the correct action as changing the managements that it has is effectively an update, even though this does not involve editing the manager record itself. The other alternative is to put it in the create action of the management controller. It is up to you which seems most appropriate. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.