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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On 1/31/07, Robert Head <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Chris Hall 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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---