Ray
Ray

Reputation: 123

search a database where column = array

i am trying to see if you can search a database with an array of strings instead of just one

if current_user.role == 'Coach' 
  @groups.each do |group| 
    @groupchats << group.id 
  end 
else 
  @mygroups.each do |group|
    @groupchats << group.group_id 
  end 
end 

@groupchatss = Chat.where("group_id= "[email protected]_s+"").order('created_at DESC')

so it searches the database where group_id= [0,1,2,3] or whatever the array actually is

i am wondering if this is actually possible

Upvotes: 0

Views: 202

Answers (2)

Rondakay
Rondakay

Reputation: 160

I believe that a linq contains would work well when searching against an array

Something like: db.mytable.where(p=>p.groupid.contains(myarray);

Upvotes: -1

Alex Kojin
Alex Kojin

Reputation: 5214

You should use sql operator IN to do this. Rails will generate right sql query for you:

@groupchatss = Chat.where(group_id: @groupchats).order('created_at DESC')

Upvotes: 2

Related Questions