假設你的模式被稱爲Event
,你可以做這樣的:
Event.where("data -> 'transition' ->> ? = ?", 'from', 'bacon')
Here is jsonb operators reference.
這個查詢將返回所有事件,其中data.transition.from
等於bacon
。
擦乾你的查詢,您可以將其添加到存儲庫,如:包含在你的模型
# app/repositories/event_repository.rb
module EventRepository
extend ActiveSupport::Concern
included do
scope :where_transition, ->(field, value) { where("data -> 'transition' ->> ? = ?", field, value) }
end
end
後:
include EventRepository
然後你可以使用它像這樣:
Event.where_transition('from', 'bacon')
Event.where_transition('to', 'ham')