如何过滤查询以显示当前用户没有标签的实例?
我试图仅显示当前用户尚未标记的品牌实例,即使其他用户已经标记了同一品牌。类似于:
控制器
这是我的控制器代码,尽管它应该是工作正常,它目前返回所有品牌实例。
@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
I'm trying to show only brand instances which the current user has not tagged, even if other users have tagged the same brand already. Something like:
Controller
This is my controller code and even though it should be working, it currently returns all brand instances.
@brand = current_user.brands.includes(:taggings).where( [ "taggings.id IS NULL OR taggings.tagger_id != ?", current_user.id ] ).order("RANDOM()").first
Schema (including my join model for good measure)
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,如果您使用acts-as-taggable-on gem 并具有以下模型:
那么您的架构中也有如下表:
那么以下 SQL 查询应该有望实现您想要的功能(?)
:进入 Rails ORM,如果不硬编码整个子选择 SQL 字符串,我就无法更接近,例如:(
我知道你可以这样做,然后使用 ruby 的 .map(&:id).join(',') 但如果这是一个大型应用程序,我认为通过将其从数据库中取出,将其转换为整数字符串并将其反馈回来(据我所知),您会损失很多性能。)
然后在您的控制器中我认为您会做类似的事情:
@brand = current_user.brands.has_not_been_tagged_by_user(current_user)
顺便说一句,我认为这实际上会执行如下 SQL(对吗?):
So if you're using the acts-as-taggable-on gem and have the following models:
So you also have the tables in your schema like:
Then the following SQL query should hopefully do what you want (?):
To translate this into Rails ORM, I can't get any closer without hard coding the whole sub-select SQL string, something like:
(I know you could do this and then use ruby's .map(&:id).join(',') but if this is a large app I think you loose a lot of performance by taking this out of the database, converting it into a string of integers and feeding it back in (as I understand it).)
Then in your controller I think you'd do something like:
@brand = current_user.brands.has_not_been_tagged_by_user(current_user)
As an aside, I think this would actually then execute an SQL like below (is that right?):
据我所知,没有 SELECT * FROM x WHERE * IS NULL 因此过程可能是唯一的方法。只需在数据库中创建一个过程并从代码中调用它,无需将 SQL 放入代码中。
您可以在此处查看此类过程的示例。
As far as I know there is no
SELECT * FROM x WHERE * IS NULL
so a procedure would probably be the only way. Just create a procedure in your DB and call it from the code, you don't have to put the SQL in the code.You can see an example of such a procedure here.