简体   繁体   English

导轨中多模型关联的最佳实现?

[英]Best implementation of a multi-model association in rails?

Alright, a Rails Noob here, :D 好吧,这里是Rails Noob,:D

It looks like has__many :through is the latest greatest way to handle many to many relationships, but I am trying to keep this simple. 看起来has__many:through是处理多对多关系的最新最佳方法,但我试图保持这种简单性。 Hopefully one of you guru's out there have handled this situation before: 希望你们中的一位大师曾经处理过这种情况:

Here is the basic model setup I have now: 这是我现在拥有的基本模型设置:

class User < ActiveRecord::Base
   has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
   has_and_belongs_to_many :users
   has_and_belongs_to_many :clients
end
class Client < ActiveRecord::Base
   has_and_belongs_to_many :products
end

Essentially, I have users in the system, that will have access (through association) to many different products that are being created, those products have many clients, but the clients can also be a part of many products, and the Products accessed by many users. 本质上,我在系统中拥有用户,这些用户可以(通过关联)访问正在创建的许多不同产品,这些产品具有许多客户端,但是这些客户端也可以是许多产品的一部分,并且许多人可以访问这些产品。用户。

All of the associations are working well, but now I want users to be able to add clients to their products, but only see clients that are associated with products they have access too. 所有关联都运行良好,但是现在我希望用户能够将客户端添加到其产品中,但只看到与他们也具有访问权限的产品相关联的客户端。

Scenario:
 Given Bob has access to product A and B
 And does NOT have access to product C
 And and has clients on product B
 And wants to add them to product A.
 When in product A Bob should see clients from product B in his add list,
 And Bob should not see clients from product C

My noobish experience with rails fails to give me the experience on how to best build the array that will hold his client list. 我对rails的no废经验未能使我获得如何最好地构建可容纳其客户列表的阵列的经验。

The way I am thinking be to use @bob.products to get the products Bob has access to then to .each those and find the clients associated with each product and then join them into a single array. 我想的方式是使用@ bob.products来获取Bob有权访问的产品,然后对每个产品进行查找,并找到与每个产品相关联的客户,然后将它们加入单个数组中。 But is this the BEST way? 但这是最好的方法吗?

Thanks! 谢谢!

Not sure if this is what you're looking for, but if you want to remove all non-authorized clients for a particular user: 不确定这是否是您要查找的内容,但是如果要删除特定用户的所有未授权客户端,请执行以下操作:

user = current_user 用户= current_user

@clients_access = Array.new @clients_access = Array.new

user.products.each { |p| user.products.each {| p | @clients_access.push(p.clients).uniq! @ clients_access.push(p.clients).uniq! } }

@clients_access.flatten! @ clients_access.flatten!

Alright so I achieved the functionality I wanted by the following: 好了,因此我实现了以下所需的功能:

user = current_user
      @clients_no_access = Client.find(:all, :order => :business_name)
      user.products.each do |product|
        @clients_no_access -= product.clients
      end
      @all_clients = Client.find(:all,
      :order => :business_name) - @clients_no_access - @product.clients

Basically, finding all the the clients, then iterating through the linked authorized products and removing them from the list, basically creating a list of non-authorized clients.. then doing the search again and clearing out the non-authorized clients and the clients already assigned in the group.. But, I have ran out of duct-tape.. any better solutions? 基本上,找到所有客户,然后遍历链接的授权产品并将其从列表中删除,基本上创建一个非授权客户的列表。然后再次进行搜索并清除非授权客户和已经存在的客户但是,我没用过胶带了。.还有更好的解决方案吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM