views:

208

answers:

1

How do I change the following bit of code so that I only have records with distinct sender_id and message_id combinations:

@roles = Role.find_all_by_simulation_id(session[:sim_id])
@messages = RolesMessages.find(:all, :conditions => ["sender_id IN (?) ", @roles.map(&:id)], :order => 'created_at DESC')
+1  A: 
@messages = RolesMessages.find(:all, :select => 'DISTINCT sender_id, message_id', :conditions => ["sender_id IN (?) ", @roles.map(&:id)], :order => 'created_at DESC')

Edit: now that I think of it, group by is probably a better option. :group => 'sender_id, message_id' will probably meet your needs better

you can also do something like:

RolesMessages.find_by_sql("query goes here")

(I prefer to make the query myself... call me crazy)

Jonathan Fingland