I have an AR descendant (job.rb) that contains a collection of other AR
has_many :target_lists, :through => :job_list_associations, :source =>
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.
ids = Array.new
self.target_lists.each do |list|
ids << list.DataSetID
#self.target_lists = Array.new - WHY DOES THIS CAUSE
list_array.each do |id|
self.target_lists << TargetList.find(id)
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?