self.id access inside model

Happy Holidays!

I have a Member.rb model with the following line:

has_many :friends, :class_name => "Member", :finder_sql => " SELECT members.* FROM members, friendships WHERE (members.id = friendships.member_id AND friendships.friend_id = #{self.id})   OR (members.id = friendships.friend_id AND friendships.member_id = #{self.id})"

When calling @member.friends, the above code translates #{self.id} to something crazy like 53931600 instead of the @member.id which is 2.

Any ideas why?

Thanks, Chad

Chad wrote:

I have a Member.rb model with the following line:

has_many :friends, :class_name => "Member", :finder_sql => " SELECT members.* FROM members, friendships WHERE (members.id = friendships.member_id AND friendships.friend_id = #{self.id}) OR (members.id = friendships.friend_id AND friendships.member_id = #{self.id})"

When calling @member.friends, the above code translates #{self.id} to something crazy like 53931600 instead of the @member.id which is 2.

Any ideas why?

Yeah, the string is evaluated when first encountered, ie when your Member class is loaded. At that point, I imagine self is the class, which is why you get a weird self.id.

To prevent this use single quotes (') around your string instead of double quotes (").