简体   繁体   English

如何通过Rails 3.2中的关联在has_many中创建记录

[英]How to create records in has_many through association in rails 3.2

I have two models (User and Event) with multiple has_many through associations, where my current association is by the below logic: 我有两个通过关联具有多个has_many的模型(用户和事件),其中当前的关联通过以下逻辑进行:

  1. User can participate in many events through EventGroup 用户可以通过EventGroup参加许多活动
  2. Event has many users through EventGroup 活动通过EventGroup有许多用户
  3. User can like many events through Eventgroup 用户可以通过事件组喜欢许多事件
  4. Event has many user likes through Eventgroup 活动通过活动组获得了许多用户的喜欢

Model: 模型:

class User
 has_many :event_groups
 has_many :events,:through => :event_groups
 has_many :event_likes,:through => :event_groups,:class_name => "Event"
end

class Event
  has_many :event_groups
  has_many :users,:through => :event_groups
  has_many :user_likes,:through => :event_groups,:class_name => "User"
end

class EventGroup
  belongs_to :user
  belongs_to :event
  belongs_to :user_like,:class_name => "User"
  belongs_to :event_like,:class_name => "Event"
end

EventGroup columns: EventGroup列:

user_id
event_id
user_like_id
event_like_id

After setting up the association I tried to create the association record with the below code: 设置关联后,我尝试使用以下代码创建关联记录:

user = User.first
user.event_likes << Event.first  
user.save

在此处输入图片说明 Here the record is created with user_id & event_like_id instead of `user_like_id & event_like_id 在这里,记录是使用user_id和event_like_id而不是`user_like_id&event_like_id创建的

But I am not able to get the User records by event.user_likes , so I checked my eventgroup record. 但是我无法通过event.user_likes获取用户记录,因此我检查了事件组记录。 It has the nil value for user_like_id. 它具有nil value for user_like_id.nil value for user_like_id.

#<EventGroup id: 24, event_id: 1, user_id: 2,event_like_id: 1, user_like_id: nil>

Let me know the proper way to do this. 让我知道执行此操作的正确方法。

Using the .where format, you can pass a string like .where("event_groups.user_like_id = ?", 17) to qualify the joined taggings table. 使用.where格式,您可以传递.where(“ event_groups.user_like_id =?”,17)之类的字符串来限定联接的标记表。

For example: 例如:

User.joins(:event_groups).where("event_groups.user_like_id = ?", 17)

I'm not sure, but you may need to define inverse_of in your has_many relations: 我不确定,但是您可能需要在has_many关系中定义inverse_of

class User
 #...
 has_many :event_likes, :through => :event_groups, :inverse_of => :user_likes, :class_name => "Event"
end

class Event
  #...
  has_many :user_likes, :through => :event_groups, :inverse_of => :event_likes, :class_name => "User"
end

See the chapter on :through in the doc of has_many 参见has_many的文档中的:through一章

It seems to me you're trying to model two many-to-many relationships (Users-Participate-In-Events and Users-Like-Events) that are distinct concepts in one relationship. 在我看来,您正在尝试对两种多对多关系(用户参与事件和用户相似事件)进行建模,它们是一种关系中的不同概念。 I'm assuming here that a User can Participate in an Event without Liking it, and vice-versa. 我在这里假设用户可以不喜欢就可以参加活动,反之亦然。 In that case, keep EventGroup modeled to only represent the Participation relationship, and create another EventLikes model to represent the Likes. 在这种情况下,将EventGroup建模为仅表示参与关系,并创建另一个EventLikes模型以表示Likes。 The columns in the two tables would be the same, but represent the different relationships. 两个表中的列将相同,但表示不同的关系。 After that, adding the Event to either (events, or event_likes) should work 之后,将事件添加到(事件或event_likes)中应该可以

我通过Relationship模型在相同模型(用户-用户)之间创建相似的关系,代码如下:

has_many :relationships, foreign_key: "follower_id", dependent: :destroy

has_many :followed_users, through: :relationships, source: :followed

has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy

has_many :followers, through: :reverse_relationships, source: :follower

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

相关问题 Rails 4:通过has_many关联创建新记录 - Rails 4: Create new records through has_many association 如何将记录添加到has_many:通过rails中的关联 - how to add records to has_many :through association in rails 如何将记录添加到has_many:通过Ruby on Rails中的关联 - how to add records to has_many :through association in Ruby on rails 如何在has_many的中间模型中创建has_many关联:以最佳方式通过关联Rails - How to create has_many associations in the intermediate model of has_many :through association in an optimal way Rails Rails-has_many:通过查找没有关联的记录 - Rails - has_many :through find records which have no association Rails:通过关联显示has_many上的所有关联记录 - Rails: Show all associated records on a has_many through association 为rails 3 has_many创建的错误数据库记录:通过关联 - incorrect database records created for rails 3 has_many :through association 将关联记录的顺序保存在Rails has_many中:通过关联 - Saving the order of associated records in a Rails has_many :through association 如何通过在rails中通过关联对has_many进行关联来实现has_many? - How do I do a has_many through association on a has_many through association in rails? 如果涉及多态关联,如何创建“ has_many through”关联? - How to create a 'has_many through' association if a polymorphic association is involved?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM