[英]Rails: Querying through 2 join table associations
I have a quite complicated relation between models and are now frustrated by a SQL Query to retrieve some objects. 我在模型之间有一个非常复杂的关系,现在对查询某些对象的SQL查询感到沮丧。
given a Product model connected to a category model via a has_many :through association and a joint table categorization . 给定一个通过has_many:through关联和联合表分类连接到类别模型的产品模型 。 Also a User model connected to this category model via a has_many :through association and a joint table *category_friendship*. 另外,一个用户模型通过has_many通过关联和联合表* category_friendship *连接到该类别模型 。
I am now facing the problem to retrieve all products, which are within the categories of the array user.category_ids. 我现在面临着检索所有产品的问题,这些产品位于数组user.category_ids的类别之内。 However, I can't just not manage to write the WHERE statement properly. 但是,我不能只是无法正确编写WHERE语句。
I tried this: 我尝试了这个:
u = User.first
uc = u.category_ids
Product.where("category_id IN (?)", uc)
However this won't work, as it doesn't have a category_id in the product table directly. 但是,这将不起作用,因为它在product表中没有直接的category_id。 But how can I change this to use the joint table categorizations? 但是,如何更改它以使用联合表分类呢?
I'm giving you the model details, maybe you find it helpful for answering my question: 我正在为您提供模型的详细信息,也许您会发现它有助于回答我的问题:
Product.rb 产品.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 Category.rb
class Category < ActiveRecord::Base
has_many :categorizations
has_many :products, through: :categorizations
has_many :category_friendships
has_many :users, through: :category_friendships
Categorization.rb 分类
class Categorization < ActiveRecord::Base
belongs_to :category
belongs_to :product
Category_friendship.rb Category_friendship.rb
class CategoryFriendship < ActiveRecord::Base
belongs_to :user
belongs_to :category
User.rb User.rb
class User < ActiveRecord::Base 类User <ActiveRecord :: 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
If you need more details to answer, please feel free to ask! 如果您需要更多详细信息来回答,请随时提问!
Looking at the associations you have defined and simplifying things. 查看您已定义并简化的关联。 Doing a bit refactoring in what we have to achieve. 对我们必须实现的内容进行一些重构。
Product.rb 产品.rb
class Product < ActiveRecord::Base
belongs_to :category
end
User.rb User.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 Category.rb
class Category < ActiveRecord::Base
has_many :users, through: :category_friendships
has_many :products
def get_products
products
end
end
NO NEED OF CREATING CATEGORY_FRIENDSHIP MODEL ONLY A JOIN TABLE IS NEEDED WITH NAME CATEGORIES_FRIENSHIPS WHICH WILL JUST HAVE USER_ID AND CATEGORY_ID CREATING CATEGORY_FRIENDSHIP模型只连接表的NO需要的是需要具有NAME CATEGORIES_FRIENSHIPS这将只是USER_ID和CATEGORY_ID
USAGE: UPDATED 用法:已更新
Controller 控制者
class UserController < ApplicationController
def index
@all_user_data = User.all_data
end
end
view index.html.erb 查看index.html.erb
<% for user in @all_user_data %>
<% for products in user.feed %>
<% for product in products %>
<%= product.name %>
end
end
end
I've upvoted Ankits answer but I realized there is a more elegant way of handeling this: 我赞成Ankits的答案,但我意识到有一种更优雅的处理方式:
given: 给出:
u = User.first
uc = u.category_ids
then I can retrieve the products out of the categories by using: 然后我可以使用以下方法从类别中检索产品:
products = Product.joins(:categories).where('category_id IN (?)', uc)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.