Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails - :joins sanitizing/substitution

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?

like image 527
Pavel K. Avatar asked Oct 06 '09 10:10

Pavel K.


1 Answers

You can use the ActiveRecord::sanitize_sql_array method.

ActiveRecord::sanitize_sql_array ['gm.group_id IN (?)', @group_ids_array]
like image 68
Damien MATHIEU Avatar answered Oct 21 '22 04:10

Damien MATHIEU