[英]What is the best way to setup my tables and relationships for this use case?
1)A user can have many causes and a cause can belong to many users. 1)一个用户可以有很多原因,一个原因可以属于许多用户。
2)A user can have many campaigns and campaigns can belong to many users. 2)一个用户可以有多个广告系列,而一个广告系列可以属于多个用户。 Campaigns belong to one cause. 竞选活动是一种原因。
I want to be able to assign causes or campaigns to a given user, individually. 我希望能够分别将原因或活动分配给给定的用户。 So a user can be assigned a specific campaign. 因此可以为用户分配特定的广告系列。 OR a user could be assigned a cause and all of the campaigns of that cause should then be associated with a user. 或可以为用户分配原因,然后应将该原因的所有广告系列与用户相关联。
Is that possible? 那可能吗? And could I set it up so that the relationships could be simplified like so: 我是否可以进行设置,以便可以简化关系,如下所示:
User.causes = all causes that belong to a user User.causes =属于用户的所有原因
User.campaigns = all campaigns that belong to user whether through a cause association or campaign association User.campaigns = 归因于原因或活动关联的所有属于用户的活动
This should work. 这应该工作。
class User < ActiveRecord::Base
has_many :causes, :through => :cause_users
has_many :campaigns, :through => :campaign_users
# other model stuff
class Cause < ActiveRecord::Base
has_many :users, :through => :cause_users
has-many :campaigns
# other model stuff
class Campaign < ActiveRecord::Base
belongs_to :cause
has_many :users, :through => :campaign_users
# other model stuff
class CampaignUser < ActiveRecord::Base
belongs_to :campaign
belongs_to :user
# other model stuff
class CauseUser < ActiveRecord::Base
belongs_to :cause
belongs_to :user
# other model stuff
has_many :through requires that you create a new model for each of these joins: campaign_users and cause_users, as is shown but it provides more functionality later on than has_and_belongs_to_many. has_many:through要求您为以下每个连接创建一个新模型:campaign_users和cause_users,如图所示,但稍后提供的功能比has_and_belongs_to_many更多。
I would also suggest using better names than :campaign_users and :cause_users so the relationship is more meaningful. 我还建议使用比:campaign_users和:cause_users更好的名称,以便使关系更有意义。
I believe you should use the following: 我相信您应该使用以下内容:
class User < ActiveRecord::Base
has_and_belongs_to_many :causes
has_and_belongs_to_many :campaigns
end
class Cause < ActiveRecord::Base
has_and_belongs_to_many :users
has_many :campaigns
end
class Campaign < ActiveRecord::Base
has_and_belongs_to_many :users
belongs_to :cause
end
This way you can use 这样你可以使用
User.causes
User.campaigns
Cause.campaing
Cause.users
Campaign.users
Campaign.cause
You can read here about has_and_belongs_to_many
relationship, here about has_one
and here about belongs_to
. 你可以阅读这里约has_and_belongs_to_many
关系, 这里大约has_one
和这里有关belongs_to
。
Let me know if this is what you want :] 让我知道这是否是您想要的东西:]
Edit: 编辑:
"I would still need User.campaigns to be campaigns from a user's causes or individual campaigns associated with a user" “我仍然需要将User.campaigns设为来自用户原因的广告系列或与该用户相关联的单个广告系列”
You can have a method on users model that returns all campaigns. 您可以在用户模型上使用一种方法来返回所有广告系列。 Something like this: 像这样:
def all_campaigns
self.campaigns + self.causes.collect{ |c| c.campaigns }
end
You can make :has_many :through associations between users and campaigns using a join model, and also between users and causes using the another join model. 您可以使用联接模型,通过用户和活动之间的关联来进行:has_many,也可以使用另一个联接模型来实现用户和原因之间的关联。 The you can make a :has_many :campaigns association in the causes model, putting a :belongs_to :cause in the campaign model. 您可以在Causes模型中建立:has_many:campaigns关联,并在广告系列模型中加入:belongs_to:cause。
But you won't be able to fetch all the users campaigns or causes by User.campaigns.orders or User.order.campaigns. 但是您将无法通过User.campaigns.orders或User.order.campaigns获取所有用户的广告系列或原因。 You should make an iteration over the User.campaigns collection or User.causes, fetching Campaign.cause or Cause.capaigns. 您应该对User.campaigns集合或User.causes进行迭代,以获取Campaign.cause或Cause.capaigns。 Or even making a custom SQL query, using joins and conditions to filter information in the joins. 甚至使用联接和条件来过滤联接中的信息,甚至进行自定义SQL查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.