hello I have a nested resource that needs the id of its parent when it is being created. This parent id is stored in a hidden attribute in the form (we''ll call this attribute A). I need this parent id to process the data from another virtual attribute on the same form (attribute B). Currently, attribute B is being processed in the model before attribute a, which is in the opposite way that I need it. So is there a way that attribute A can be processed before attribute B? I have tried rearranging the code putting attribute A''s method above B''s and it hasn''t work. Or is there a better way to discover the parent id before the child is saved? Thanks, Mike -- 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 -~----------~----~----~----~------~----~------~--~---
On Sep 12, 8:50 pm, Michael Lavrisha <rails-mailing-l...@andreas- s.net> wrote:> hello > > I have a nested resource that needs the id of its parent when it is > being created. This parent id is stored in a hidden attribute in the > form (we''ll call this attribute A). I need this parent id to process the > data from another virtual attribute on the same form (attribute B). > > Currently, attribute B is being processed in the model before attribute > a, which is in the opposite way that I need it. So is there a way that > attribute A can be processed before attribute B? I have tried > rearranging the code putting attribute A''s method above B''s and it > hasn''t work. >I take it that you''re just passing a hash to update_attributes. You can''t rely on a hash being in any particular order. Your best bet is to pull out of the hash the value that needs to be set first and set it yourself. Fred> Or is there a better way to discover the parent id before the child is > saved? > > Thanks, > Mike > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Sep 12, 8:50�pm, Michael Lavrisha <rails-mailing-l...@andreas- > Your best bet is > to pull out of the hash the value that needs to be set first and set > it yourself. > > FredHow do you access the hash from the form? I know you can just get it from params in the controller but I don''t know how to access it in the model. -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 13 Sep 2008, at 21:30, Michael Lavrisha wrote:> > Frederick Cheung wrote: >> On Sep 12, 8:50�pm, Michael Lavrisha <rails-mailing-l...@andreas- >> Your best bet is >> to pull out of the hash the value that needs to be set first and set >> it yourself. >> >> Fred > > How do you access the hash from the form? I know you can just get it > from params in the controller but I don''t know how to access it in the > model.I was thinking along the lines of def some_action @record = Record.find[:id] @record.some_attribute = params[:record].delete :some_attribute @record.update_attributes! params[:record] end Fred> > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 13 Sep 2008, at 21:30, Michael Lavrisha wrote: > >> from params in the controller but I don''t know how to access it in the >> model. > > I was thinking along the lines of > > def some_action > @record = Record.find[:id] > @record.some_attribute = params[:record].delete :some_attribute > @record.update_attributes! params[:record] > end > > FredYeah, that is essentially what I want to do but I don''t know the syntax to do it. So in my view I have: translations/new.html.erb <%= hidden_field_tag ''translation[project]'', params[:project_id] %> Then in the model: translation.rb class Translation < ActiveRecord::Base belongs_to :project def attribute_a=(project) @project = project.id end def attribute_b=(attr_b) # this is where I need @project or the attribute_a value -- 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 -~----------~----~----~----~------~----~------~--~---
On Sep 14, 1:22 am, Michael Lavrisha <rails-mailing-l...@andreas- s.net> wrote:> Frederick Cheung wrote: > > On 13 Sep 2008, at 21:30, Michael Lavrisha wrote: > > >> from params in the controller but I don''t know how to access it in the > >> model. > > > I was thinking along the lines of > > > def some_action > > @record = Record.find[:id] > > -KopBYAnOfQD/9pzu0YdTqQ@public.gmane.org_attribute = params[:record].delete :some_attribute > > -y5Ho/ty3wIHTrTC9+X8o8w@public.gmane.org_attributes! params[:record] > > end > > > Fred > > Yeah, that is essentially what I want to do but I don''t know the syntax > to do it. >I''ve just shown you how to do it :) Replace :record with :translation and :some_attribute with :project and you''ll be on your way (you just don''t need the .id since what you''re getting is an id already). However I''m confused why you have this accessor at all. Sure the project_id value should just be stored in the project_id column ? Fred> So in my view I have: > translations/new.html.erb > <%= hidden_field_tag ''translation[project]'', params[:project_id] %> > > Then in the model: > translation.rb > class Translation < ActiveRecord::Base > belongs_to :project > > def attribute_a=(project) > @project = project.id > end > > def attribute_b=(attr_b) > # this is where I need @project or the attribute_a value > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---