sql - How to get the exact matched record in rails 3? -


items in database:

 => [#<item id: 1, item_collection: [:laptop, :computer, :mouse, :speaker]>,       #<item id: 2, item_collection: [:laptopandkeyboard, :disk, :screen]>]  

below query give me both records (which include laptop , laptopandkeyboard).

item.where("item_collection ?", "%laptop%")   => [#<item id: 1, item_collection: [:laptop, :computer, :mouse, :speaker]>,       #<item id: 2, item_collection: [:laptopandkeyboard, :disk, :screen]>]  

but want record has laptop only.

#<item id: 1, item_collection: [:laptop, :computer, :mouse, :speaker]> 

serialized arrays add \n @ end of each item, can try this:

item.where("item_collection ?", "%laptop\n%") 

another option fetch data , filter select, this:

item.all.select { |item| item.item_collection.include?(:laptop) } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -