【问题标题】:Rails: Querying through 2 join table associationsRails:通过 2 个连接表关联进行查询
【发布时间】:2012-08-29 00:33:24
【问题描述】:

我在模型之间有一个相当复杂的关系,现在对检索一些对象的 SQL 查询感到沮丧。

给定一个产品模型,通过一个has_many :through 关联和一个联合表分类连接到一个类别模型。 还有一个 User 模型 通过 has_many :through 关联和联合表 *category_friendship* 连接到此 category 模型。

我现在面临检索所有产品的问题,这些产品在数组 user.category_ids 的类别中。但是,我不能只是无法正确编写 WHERE 语句。

我试过了:

u = User.first
uc = u.category_ids
Product.where("category_id IN (?)", uc)

但这不起作用,因为它在产品表中没有直接的 category_id。但是如何更改它以使用联合表分类?

我正在给你模型的详细信息,也许你会发现它有助于回答我的问题:

Product.rb

class Product < ActiveRecord::Base

 belongs_to :category

 def self.from_users_or_categories_followed_by(user)
 cf = user.category_ids
 uf = user.friend_ids

 where("user_id IN (?)", uf) # Products out of friend_ids (uf) works fine, but how to extend to categories (cf) with an OR clause?
 end

Category.rb

class Category < ActiveRecord::Base
 has_many :categorizations
 has_many :products, through: :categorizations
 has_many :category_friendships
 has_many :users, through: :category_friendships

分类.rb

class Categorization < ActiveRecord::Base

 belongs_to :category
 belongs_to :product

Category_friendship.rb

class CategoryFriendship < ActiveRecord::Base

 belongs_to :user
 belongs_to :category

用户.rb

类用户<:base>

has_many :category_friendships
has_many :categories, through: :category_friendships

def feed
 Product.from_users_or_categories_followed_by(self) #this should aggregate the Products
end

如果您需要更多详细信息来回答,请随时询问!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 activerecord


    【解决方案1】:

    查看您定义的关联并简化事情。对我们必须实现的目标进行一些重构。

    Product.rb

    class Product < ActiveRecord::Base
    
      belongs_to :category
    
     end
    

    用户.rb

      class User < ActiveRecord::Base
            has_many :categories, through: :category_friendships
            scope :all_data , includes(:categories => [:products])
    
       def get_categories
         categories
       end
    
       def feed
          all_products = Array.new
          get_categories.collect {|category| category.get_products }.uniq
       end
      end
    

    Category.rb

    class Category < ActiveRecord::Base
     has_many :users, through: :category_friendships
     has_many :products
    
     def get_products
       products
     end
    end
    

    无需创建 CATEGORY_FRIENDSHIP 模型,只需要一个名为 CATEGORIES_FRIENSHIPS 的联接表,该表将只有 USER_ID 和 CATEGORY_ID

    用法:更新

    控制器

      class UserController < ApplicationController
             def index
               @all_user_data = User.all_data
            end
       end
    

    查看 index.html.erb

    <% for user in @all_user_data %>
     <% for products in user.feed %>
      <% for product in products %>
           <%= product.name %>
         end
      end
    end
    

    【讨论】:

    • 感谢您的回答。我试过了,但我没有看到这解决了我的问题:例如:假设“categories_first_user”检索类别 3 和 4。然后你的最后一行将向我显示类别 3 中的产品,但错过了类别 4 中的产品。我认为应该有类似 categories_first_user.products 或 .all.products 的东西,但这让我遇到了未定义的方法错误。我也很难想象如何在我的 Product 模型中的方法“self.from_users_and_categories_followed_by(user)”中的 where 语句中包含您的方法。
    • aha.. 尝试:首先初始化 products_array = Array.new 然后执行 categories_first_user.collect {|category| products_array
    • 谢谢@Ankit!它似乎有效(我想我有一些双胞胎)。但是我看不到如何将其应用于我的方法,因此我可以检索用户的朋友产品(使用 Product.rb 中的 where 语句)和/或用户的类别产品(您的解决方案)。你知道如何处理这个吗?谢谢!!
    • 处理双峰值简单做 categories_first_user.collect {|category| products_array 一个 uniq 结尾
    • @StonJarks:如果它对您有帮助,请不要忘记为解决方案投票 :)
    【解决方案2】:

    我赞成 Ankits 的回答,但我意识到有一种更优雅的方式来处理这个问题:

    给定:

    u = User.first
    uc = u.category_ids
    

    然后我可以使用以下方法从类别中检索产品:

    products = Product.joins(:categories).where('category_id IN (?)', uc)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      相关资源
      最近更新 更多