1

タグのないエントリを見つける正しい方法(tm)は何ですか?

使ってみEntry.tagged_with(nil)ましたが、空のハッシュを返すだけです。

まだタグ付けする必要があるエントリをリストできるようにするために必要です。

ありがとう。

4

4 に答える 4

5

以下は私のために働くようです:

Entry.includes("taggings").where("taggings.id is null")

これは連鎖もサポートするはずです。たとえば、次のように動作するはずです。

Entry.includes("taggings").where("taggings.id is null").where(foo: "bar")
于 2013-09-06T09:15:05.777 に答える
3

タグ付けされた写真とタグ付けされていない写真を見つけるための私の解決策は次のとおりです。

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 モデルでは機能しますが、他のスコープとチェーンすることはできません。

于 2013-05-21T04:04:16.407 に答える
2

これは古いことだと思いますが、今日も同様の必要性が生じました. 単純な複数クエリスコープでそれを行いました:

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)
}

巨大なデータセットには効率的ではないかもしれませんが、私の目的には合っています。

于 2012-03-05T20:53:28.150 に答える
0

タグ付け可能な行為の内部を知らなければ、クエリのループや生のSQLを含まないきちんとした方法を考えることはできません。これは読み取り可能です:

need_to_tag = []

Entry.all.each do |e|
  need_to_tag << e if e.tag_list.blank?
end

need_to_tagは、タグなしのエントリを保持します。

于 2011-01-01T15:55:55.840 に答える