1

我试图只显示当前用户没有标记的品牌实例,即使其他用户已经标记了同一个品牌。就像是:

在此处输入图像描述

控制器

这是我的控制器代码,即使它应该可以工作,但它当前会返回所有品牌实例。

@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
4

2 回答 2

2

因此,如果您使用acts-as-taggable-on gem 并具有以下模型:

class User < ActiveRecord::Base
  acts_as_tagger
  has_many :brand_users
  has_many :brands, :through => :brand_users
end

因此,您的架构中也有表,例如:

create_table "users", :force => true  do |t|
  t.string "name"
end

create_table "brands", :force => true  do |t|
  t.string "name"
end

那么下面的 SQL 查询应该有希望做你想要的(?):

SELECT brands.*
FROM brands
WHERE brands.id NOT IN (
    SELECT brands.id
    FROM brands
    INNER JOIN brand_users ON brand_users.brand_id = brands.id
    INNER JOIN taggings ON (taggings.tagger_id = brand_users.user_id AND taggings.tagger_type = 'User')
    WHERE brand_users.user_id = 1 AND taggings.taggable_id = brand_users.brand_id
)

要将其转换为 Rails ORM,如果不对整个子选择 SQL 字符串进行硬编码,我将无法接近,例如:

class Brand < ActiveRecord::Base
  has_many :brand_users
  has_many :users, :through => :brand_users

  scope :has_not_been_tagged_by_user, lambda {|user| where("brands.id NOT IN (SELECT brands.id
    FROM brands
    INNER JOIN brand_users ON brand_users.brand_id = brands.id
    INNER JOIN taggings ON (taggings.tagger_id = brand_users.user_id AND taggings.tagger_type = 'User')
    WHERE brand_users.user_id = ? AND taggings.taggable_id = brand_users.brand_id)", user.id) }

end

(我知道你可以这样做,然后使用 ruby​​ 的.map(&:id).join(',')但如果这是一个大型应用程序,我认为你通过将其从数据库中取出并转换它会损失很多性能成一串整数并将其反馈回(据我所知)。)

然后在您的控制器中,我认为您会执行以下操作:

@brand = current_user.brands.has_not_been_tagged_by_user(current_user)

顺便说一句,我认为这实际上会执行如下 SQL(对吗?):

SELECT brands.*
FROM users
INNER JOIN brand_users ON brand_users.user_id = users.id
INNER JOIN brands ON brands.id = brand_users.brand_id 
WHERE brands.id NOT IN (
    SELECT brands.id
    FROM brands
    INNER JOIN brand_users ON brand_users.brand_id = brands.id
    INNER JOIN taggings ON (taggings.tagger_id = brand_users.user_id AND taggings.tagger_type = 'User')
    WHERE brand_users.user_id = 1 AND taggings.taggable_id = brand_users.brand_id
) AND users.id = 1
于 2011-08-30T00:27:26.320 回答
0

据我所知,没有SELECT * FROM x WHERE * IS NULL这样的程序可能是唯一的方法。只需在您的数据库中创建一个过程并从代码中调用它,您不必将 SQL 放入代码中。

您可以在此处查看此类过程的示例。

于 2011-08-29T19:54:43.747 回答