假设你的模式被称为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')