I am a ruby on Rails Novice and I am having an extremely frustrating problem - not sure whether acts_as_tree plug in is causing this or not (or me lacking a fundamental misunderstanding of rails) I have the following table schema: create_table "destinations", :force => true do |t| t.string "name" t.string "description" t.integer "commissions_rate", :default => 0 t.integer "destinations_count", :default => 0 t.integer "position" t.datetime "created_at" t.datetime "updated_at" end and model def: class Destination < ActiveRecord::Base attr_accessible :id, :name, :parent_id, :centre_longitude, :centre_latitude, :commissions_rate, :position, :startzoom, :description, :destinations_count acts_as_tree :counter_cache => true end I am trying to adjust the update method in my controller so that if the parent commissions_rate is modified the "children" records of the parent are automatically updated with this value, code as follows: def update @destination = Destination.find(params[:id]) @rate = @destination.commissions_rate || 0 @children = @destination.children if (@destination.destinations_count > 0) for child in @children do child.commissions_rate = @rate end end respond_to do |format| if @destination.update_attributes(params[:destination]) flash[:notice] = ''Destination was successfully updated.'' ......... end The form only send data for the parent model (not using nested model) Everything works when I test the above code in the Rails console however when running the server the children are failing to have the commission_rate variable updated. Would appreciate any help anyone can provide me. Thanks David -- 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 15 February 2010 10:46, David Krett <dbkrett-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I am a ruby on Rails Novice and I am having an extremely frustrating problem - not sure whether acts_as_tree plug in is causing this or not (or me lacking a fundamental misunderstanding of rails) > > Everything works when I test the above code in the Rails console however when running the server the children are failing to have the commission_rate variable updated. > Would appreciate any help anyone can provide me. >You''re not saving the child after you update it''s value for child in @children do child.commissions_rate = @rate child.save end or for child in @children do child.update_attribute(:commissions_rate, @rate) end .update_attribute automatically saves, check the Rails API docs for more info on using this method. PS You''re using a lot of instance variables there, which seem like there would be better scoped as local variables (ie drop the @ from the variables unless you need them to be accessible on an instance of the object - @destination is probably the only legitimate instance variable) -- 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.
Many thanks Michael, it solved the problem, and thanks for the extra advice I need it! as where I am based (Indonesia) no one teaches rails. On Feb 15, 7:39 pm, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 15 February 2010 10:46, David Krett <dbkr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I am a ruby on Rails Novice and I am having an extremely frustrating problem - not sure whether acts_as_tree plug in is causing this or not (or me lacking a fundamental misunderstanding of rails) > > > Everything works when I test the above code in the Rails console however when running the server the children are failing to have the commission_rate variable updated. > > Would appreciate any help anyone can provide me. > > You''re not saving the child after you update it''s value > > for child in @children do > child.commissions_rate = @rate > child.save > end > > or > > for child in @children do > child.update_attribute(:commissions_rate, @rate) > end > > .update_attribute automatically saves, check the Rails API docs for > more info on using this method. > > PS You''re using a lot of instance variables there, which seem like > there would be better scoped as local variables (ie drop the @ from > the variables unless you need them to be accessible on an instance of > the object - @destination is probably the only legitimate instance > variable)-- 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.