Forum: Ruby on Rails Updating an attribute in all elements of acts_as_list

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
pergesu (Guest)
on 2005-11-19 13:26
(Received via mailing list)
I'm whipping up a basic ticketing system, and I've got a Ticket model
that uses acts_as_list:

class Ticket < ActiveRecord::Base

	belongs_to :parent, :class_name => "Ticket", :foreign_key =>
"parent_id"
	has_many :replies, :class_name => "Ticket", :foreign_key =>
"parent_id", :order => :position

	acts_as_list :scope => :parent_id

end

It has a boolean field named resolved, and when I update it in the
parent ticket, I'd like the changes to be propogated down through the
replies.  To do this, I overloaded the resolved= method:

	def resolved=(r)
		super(r)
		replies.each {|reply| reply.resolved = r}
	end

Here are a few assertions that all pass:
  	t = Ticket.find(1001)
  	assert !t.resolved?
  	t.resolved = true
  	assert t.resolved?
  	assert t.replies[0].resolved?

However, t.replies[0].parent.resolved? returns false, which makes no
sense to me, because the actual object that parent refers to returns
true.

If I save and reload the parent ticket, t.replies[0].resolved? returns
false.  Okay, this makes sense because I didn't save each individual
ticket.  So I created a new method, Ticket.resolve, which takes the
place of resolved=

	def resolve
		setresolved(true)
	end

	protected
	def setresolved(status)
		Ticket.transaction(self, self.replies) do
			self.resolved = status
			puts self.resolved?
			replies.each {|r| r.resolved = status}
		end
	end

I get the exact same deal if I use this instead...t.resolved? and
t.replies[0].resolved? return true before the reload, but if I reload
the t object then replies[0].resolved? returns false.

I don't really know what to try anymore.  First of all, I'm confused
how t.resolved? could be true and t.replies[0].parent.resolved? could
be false, since they're the same object.  Secondly, I'd just like to
be able to update the 'resolved' attribute in all the children of the
root object.

Thanks for taking the time to read all this, I'd appreciate any help.

Pat
This topic is locked and can not be replied to.