Collection management on form updates - problem with updatin

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs