タグのないエントリを見つける正しい方法(tm)は何ですか?
使ってみEntry.tagged_with(nil)
ましたが、空のハッシュを返すだけです。
まだタグ付けする必要があるエントリをリストできるようにするために必要です。
ありがとう。
タグのないエントリを見つける正しい方法(tm)は何ですか?
使ってみEntry.tagged_with(nil)
ましたが、空のハッシュを返すだけです。
まだタグ付けする必要があるエントリをリストできるようにするために必要です。
ありがとう。
以下は私のために働くようです:
Entry.includes("taggings").where("taggings.id is null")
これは連鎖もサポートするはずです。たとえば、次のように動作するはずです。
Entry.includes("taggings").where("taggings.id is null").where(foo: "bar")
タグ付けされた写真とタグ付けされていない写真を見つけるための私の解決策は次のとおりです。
scope :with_taggings, where('id in (select taggable_id from taggings where taggable_type = ’Photo‘ and context = ’bibs‘)')
scope :without_taggings, where('id not in (select taggable_id from taggings where taggable_type = ’Photo‘ and context = ’bibs‘)')
ただし、Photo モデルでは機能しますが、他のスコープとチェーンすることはできません。
これは古いことだと思いますが、今日も同様の必要性が生じました. 単純な複数クエリスコープでそれを行いました:
scope :untagged, lambda {
# first build a scope for the records of this type that *are* tagged
tagged_scope = Tagging.where(:taggable_type => base_class.to_s)
# only fetching the taggable ids
tagged_scope = tagged_scope.select('DISTINCT taggable_id')
# and use it to retrieve the set of ids you *don't* want
tagged_ids = connection.select_values(tagged_scope.to_sql)
# then query for the rest of the records, excluding what you've found
where arel_table[:id].not_in(tagged_ids)
}
巨大なデータセットには効率的ではないかもしれませんが、私の目的には合っています。
タグ付け可能な行為の内部を知らなければ、クエリのループや生のSQLを含まないきちんとした方法を考えることはできません。これは読み取り可能です:
need_to_tag = []
Entry.all.each do |e|
need_to_tag << e if e.tag_list.blank?
end
need_to_tagは、タグなしのエントリを保持します。