0
にコードに従って、このように書きます:続編は「スコープ」を追加し、条件付き
class Foo < Sequel::Model
self.dataset_module do
def property_active
where('properties @> \'{"active": true}\'')
end
end
end
class Bar
def foo_ids
Foo.select(:other_model_id).distinct.all
end
def condition?
...
end
end
私はcondition?
方法Oによってスコープproperty_active
を追加するfoo_ids
方法を変更したいです。
Foo.select(:other_model_id).where{ Foo.property_active if condition? }.distinct
をそして、それは返します:これまでのところ私はこれを考え出す
# when condition? is true
=> "SELECT DISTINCT \"other_model_id\" FROM \"foos\" WHERE (SELECT * FROM \"foos\" WHERE (properties @> '{\"active\": true}'))"
# when condition? is false
=> "SELECT DISTINCT \"other_model_id\" FROM \"foos\""
コードが良いですが、私はSELECT
WHERE
後、そこにあることを持っていることの考えを好きではありませんtrue
のケースについて素敵なクエリを返すには?何かのように:
"SELECT DISTINCT \"other_model_id\" FROM \"foos\" WHERE (properties @> '{\"active\": true}')"
これはうまくいけば、これを行うための続編があります。 – zetacu
@zetacuあなたが探しているものが異なっているかどうか不明です。私が使ったコンポジションスタイルは[Sequelの紹介](https://github.com/jeremyevans/sequel#an-introduction)でも使われています。 – coreyward
'Model.query.conditional_query.query'のように、' Foo.where(bar: 'somethig')のようなクエリを入れ子にするような、ある種の続編メソッドがあるかどうかを調べていたのですが、some_conditional_restriction_sequel_method.where(foo: '何か').all'という方法で、より機能的なプログラミングに追加することができます。スコープを追加する唯一の方法は、スコープを追加する唯一の方法だと思っていますが、その続編が既に組み込まれています。 – zetacu