Class instance methods in :conditions for AR associations

5 views
Skip to first unread message

Chris

unread,
Aug 19, 2006, 1:15:32 AM8/19/06
to rubyonra...@googlegroups.com
I am trying to create a special active record assocation where I use a
class instance method inside the association. Is this possible to do?

Example:

Person
{
id : integer
name : string
age : integer
}

Pet
{
id : integer
name : string
species : string
age : integer
person_id : integer
}


class Person < ActiveRecord::Base

has_many_friends :pets

has_many :pets_older_than_self,
:class_name => "Pet"
:conditions => "age = #{self.age}"

end

Can I use the person's age in the condition? I would think since the
generated sql will use the condition that the pet's person id = the id
of the current person, it could also use other instance methods or
variables. However I can't seem to get this to work.

Thank you for any help you can provide.

Chris

--
Posted via http://www.ruby-forum.com/.

Reply all
Reply to author
Forward
0 new messages