views:

169

answers:

1

is it possible to specify parameters for :joins similar way as for :conditions?

here is an example (sql code is irrelevant)

named_scope :threads, {
  :joins => [" LEFT JOIN groups_messages gm ON messages.id=gm.message_id AND gm.group_id IN (?) ",@group_ids_array], 
  :conditions => ["creator_id=? AND messages.id IN (?)", current_user_id, @message_ids_array]
}

in this example parameters for :conditions would get inserted properly, but for :joins i would get an error

 Association named '  LEFT JOIN groups_messages gm ON messages.id=gm.message_id and gm.group_id IN (?) ' was not found; perhaps you misspelled it?

what function substitutes parameters for :conditions?

+2  A: 

You can use the ActiveRecord::sanitize_sql_array method.

ActiveRecord::sanitize_sql_array ['gm.group_id IN (?)', @group_ids_array]
Damien MATHIEU
thank you so much! i was stuck on that for about a day
Pavel K.