Has_many :conditions

Is it possible to put conditions relative to the current instance in
has_many’s :conditions?

For example, in a forum app:

class Post < ActiveRecord::Base

belongs_to :topic

has_many :self_and_descendants, :class_name => “Post”, :order =>
“lft”,
:conditions => “topic_id = #{topic_id} AND (lft BETWEEN #{lft} AND
#{rgt})”

end

With the above code, I get the following exception:

undefined local variable or method `topic_id’ for Post:Class

I’m not interested in whether acts_as_nested_set is a good idea or
already has this functionality. It’s just an example.

So, can you put instance attributes in the :conditions? If so, how?

Many thanks,
Rob Head

if you want to use something like #{…} in an association condition,
:order, etc, you need to use singel quotes around the sql fragment.
the reason being is that the string is being interpreted when the
class is loaded and it doesn’t know what topic_id is. #{} is not
interpreted inside single quotes.

Chris H. wrote:

if you want to use something like #{…} in an association condition,
:order, etc, you need to use singel quotes around the sql fragment.
the reason being is that the string is being interpreted when the
class is loaded and it doesn’t know what topic_id is. #{} is not
interpreted inside single quotes.

Sweet!

(In retrospect, duh.)

Thanks, much!