简体   繁体   中英

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:

  1. User can participate in many events through EventGroup
  2. Event has many users through 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:

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

But I am not able to get the User records by event.user_likes , so I checked my eventgroup record. It has the 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.

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:

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

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. 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

我通过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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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