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
Post a Comment