I have an AR descendant (job.rb) that contains a collection of other AR models. has_many :target_lists, :through => :job_list_associations, :source => :target_list I have a form that I use to update the target_lists array. Here are the two methods for "target_list_ids" which I use to manage the form data for job. def target_list_ids ids = Array.new self.target_lists.each do |list| ids << list.DataSetID end ids end public def target_list_ids=(list_array) #self.target_lists = Array.new - WHY DOES THIS CAUSE PROBLEMS?!!?!!? self.target_lists.clear list_array.each do |id| self.target_lists << TargetList.find(id) end end On the post, when target_list_ids is called, if I use "clear" on the target_lists array and then set whatever came in on the form, everything works fine and I see my changes on the form. However, if I try to assign self.target_lists to a new Array object and do the setting, I never see the changes. When the form redisplays (assuming another error on the page, I see the form without any list changes I made). This _almost_ makes sense to me, but I wondered if someone could explain why setting self.target_lists to be a new Array object seems to ensure that none of the changes are stored in the job object. Is it because the "attribute" target_lists is not being updated when I create the new array? And if I simply manipulate the existing collection (via clear), I''m directly updating the "attribute" target_lists? Thanks, Wes --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Wes Gamble
2006-Sep-25 18:38 UTC
Re: Collection management on form updates - problem with upd
Does the issue with resetting my collection to a new Array stem from the fact that I''m not setting the join attribute on the elements that I''m adding to the collection? The thing about that is this is a _new_ Job object so there isn''t anything in the database, so I wouldn''t think that the absence of join attributes would be an issue - there shouldn''t be any foreign key values anyway. WG -- 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 -~----------~----~----~----~------~----~------~--~---