Reputation: 34023
I have a json column - document
.
I have specified the color_code
attribute with store_accessor :document, :color_code
I can successfully query all instances for the number 3:
MyModel.where("document ->> 'color_code' = '3'")
But how would I convert that query to a scope with a number argument, so I can use it more generally?
Upvotes: 3
Views: 1268
Reputation: 16507
You can try something like this:
class MyModel
scope :with_color, ->(color) { where("document ->> 'color_code' = '?'", color) }
end
and then issue the scope:
MyModel.with_color(3)
Upvotes: 7