[英]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的模型(用户和事件),其中当前的关联通过以下逻辑进行:
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.