【发布时间】:2011-08-29 18:16:00
【问题描述】:
我试图仅显示当前用户未标记的品牌实例,即使其他用户已经标记了同一品牌。比如:
控制器
这是我的控制器代码,尽管它应该可以工作,但它当前会返回所有品牌实例。
@brand = current_user.brands.includes(:taggings).where( [ "taggings.id IS NULL OR taggings.tagger_id != ?", current_user.id ] ).order("RANDOM()").first
架构(包括我的连接模型)
create_table "brand_users", :force => true do |t|
t.integer "brand_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "taggings", :force => true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context"
t.datetime "created_at"
end
add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context"
create_table "tags", :force => true do |t|
t.string "name"
end
end
【问题讨论】:
-
谢谢,虽然我不太愿意在控制器中使用纯 SQL。
-
据我所知没有
SELECT * FROM x WHERE * IS NULL,所以程序可能是唯一的方法。只需在您的数据库中创建一个过程并从代码中调用它,您不必将 SQL 放入代码中。 -
是否愿意将其添加为答案?
-
你看到这个答案了吗?:stackoverflow.com/questions/1314408/…
标签: sql ruby ruby-on-rails-3 tagging acts-as-taggable-on