Hello,
I''ve been spinning arround an issue for days and simply
don''t know
how 11`to solve it in rails.
I''m sure there is a simple solution for my problems once this is a
very common situation.
Well, I have a has_many/belongs_to association in my project that
goes like this:
Farm < ActiveRecord::Base
has_many :parties, :dependent => ''delete_all''
end
Party < ActiveRecord::Base
belongs_to :farm
end
OK, The creation and deletion of the parties goes very well,
including validations and etc. My problem is regarding to updating the
detail records. I simply didn''t get how to do it. It seems to me that
simple assignment don''t work... I think that @farm.parties= only
create new records and delete those that are no longer assigned.
My questions are:
1) How can I update information in child records?
For this one I did something like
params[:farm][''parties''].each do |p|
p.save
end
2) I did the following to "pass" the errors from the detail to the
master:
params[:farm][''parties''].each do |p|
if !p.save
@farm.errors.add_to_base(''oh no!!'')
end
end
OK, the problem is that when some error occur, when the edit action
is rendered again, it is shown with the old values, the ones that are
in the DB.
It is late at night and I am sorry if my explanations are not enough.
Thank you all in advance.
--
Ricardo Acras
ricardo-WlbSPrj98RBeoWH0uzbU5w@public.gmane.org
Acras Desenvolvimento de Sistemas
+55+41-3232-6404
www.acras.net
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
I think you want @farm.parties << a_party @farm.save On Dec 5, 3:06 pm, "Ricardo Acras" <ricardo.ac...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > I''ve been spinning arround an issue for days and simply don''t know > how 11`to solve it in rails. > I''m sure there is a simple solution for my problems once this is a > very common situation. > Well, I have a has_many/belongs_to association in my project that > goes like this: > > Farm < ActiveRecord::Base > has_many :parties, :dependent => ''delete_all'' > end > > Party < ActiveRecord::Base > belongs_to :farm > end > > OK, The creation and deletion of the parties goes very well, > including validations and etc. My problem is regarding to updating the > detail records. I simply didn''t get how to do it. It seems to me that > simple assignment don''t work... I think that @farm.parties= only > create new records and delete those that are no longer assigned. > My questions are: > > 1) How can I update information in child records? > For this one I did something like > params[:farm][''parties''].each do |p| > p.save > end > > 2) I did the following to "pass" the errors from the detail to the master: > params[:farm][''parties''].each do |p| > if !p.save > @farm.errors.add_to_base(''oh no!!'') > end > end > OK, the problem is that when some error occur, when the edit action > is rendered again, it is shown with the old values, the ones that are > in the DB. > > It is late at night and I am sorry if my explanations are not enough. > > Thank you all in advance. > > -- > Ricardo Acras > rica...-WlbSPrj98RBeoWH0uzbU5w@public.gmane.org > Acras Desenvolvimento de Sistemas > +55+41-3232-6404www.acras.net--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ricardo Acras wrote:> 1) How can I update information in child records? > For this one I did something like > params[:farm][''parties''].each do |p| > p.save > endIn Farm model, add a :before_update callback that saves existing parties, i.e. parties.each {|p| p.save unless p.new_record?}> 2) I did the following to "pass" the errors from the detail to the > master: > params[:farm][''parties''].each do |p| > if !p.save > @farm.errors.add_to_base(''oh no!!'') > end > endTry using validates_associated Regards Massimo http://www.addsw.it -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---