查询自:通过协会
Querying From :through Association
我正在创建一个 rails 应用来学习 has_many:通过协会...我知道我没有遵循 rails 惯例,但那是因为我想学习所有Active Record 必须提供的不同选项。
我的问题是,我想我已经创建了关系,但我不知道如何建立邀请并查询它们以获得与会者,并且 attended_events。我必须先创建邀请吗?如果是这样,我如何将它与事件相关联?那么,如何才能让很多用户参加活动呢?这些问题对你们中的一些人来说可能是显而易见的,但我却难以理解。有没有人愿意给我一个基本的 运行 下来?我是否正确设置了我的代码以获得我想要的结果?这是我目前的设置:
class Invite < ActiveRecord::Base
belongs_to :attending_guest, :class_name => "User"
belongs_to :attending_event, :class_name => "Event"
end
class User < ActiveRecord::Base
has_many :created_events, :foreign_key => "creator_id", :class_name => "Event"
has_many :invites, :foreign_key => :attending_guest_id
has_many :attended_events, through: :invites, source: :attending_event
end
class Event < ActiveRecord::Base
belongs_to :creator, :class_name => "User"
has_many :invites, :foreign_key => :attending_event_id
has_many :attendees, :through => :invites, :source => :attending_guest
end
基本上,一个事件有一个创建者,我想我已经正确地完成了这一部分。接下来,我希望能够获得参加该活动的用户列表。另外,我希望能够看到用户将要参加的活动。但是我什至如何建立一个邀请并让一个事件与一群用户相关联,rails 如何处理这个?如果有人能解释一下我如何做这些事情并给我一些 clarifications/tips,我将不胜感激。谢谢!
你能单独创建每个 ActiveModel 的实例并保存吗?确保你的外键是正确的。
如果关系等是正确的,我相信(伪)代码应该是这样的:
creator = User.where(...
event = Event.new
event.save! # You need to save before adding to association
user = User.where(...
user.invites.create!(creator_id: creator)
除非我遗漏了什么,否则应该大致如此。不过,请务必将其包装在交易中。
更新:我认为你应该可以在没有交易的情况下做到这一点:
creator = User.where(...
event = Event.new
user = User.where(...
user.invites.build(creator_id: creator)
event.save!
这应该有效并且更好,因为它允许您在 Event 中使用验证器来检查是否存在所需的关联。
使用 belongs_to
、has_many
等为您提供了一些非常方便的方法来将对象相互关联起来。例如 belongs_to
附带:
1. association
2. association=(associate)
3. build_association(attributes = {})
4. create_association(attributes = {})
5. create_association!(attributes = {})
到目前为止,你的问题是如何构建邀请并将它们与事件和用户相关联,这是我的建议:
由于在模型 Invite
中您使用 belongs_to
定义了 2 个关联,您可以使用上面列表中的方法 No.5
如下:
invite = Invite.create!
invite.create_attending_guest!(<the necessary attributes and respective values for creating a new user>)
invite.create_attending_event!(<the necessary attributes and respective values for creating a new event>)
或反过来:
guest = User.create!(<attrs>)
event = Event.create!(<attrs>)
invite = Invite.create!(attending_guest: guest, attending_event: event)
I want to be able to see what events a user is going to
您可以像这样访问它们:
u = User.find(5)
events = u.attended_events
how do I have one event be associated with a bunch of users
在这种情况下,您可以使用 has_many
添加的方法 <<
(其他请参见 here):
u1 = User.create!(<attrs>)
u2 = User.create!(<attrs>)
u3 = User.create!(<attrs>)
event = Event.create!(<attrs>)
event.attendees << u1
event.attendees << u2
event.attendees << u3
我正在创建一个 rails 应用来学习 has_many:通过协会...我知道我没有遵循 rails 惯例,但那是因为我想学习所有Active Record 必须提供的不同选项。
我的问题是,我想我已经创建了关系,但我不知道如何建立邀请并查询它们以获得与会者,并且 attended_events。我必须先创建邀请吗?如果是这样,我如何将它与事件相关联?那么,如何才能让很多用户参加活动呢?这些问题对你们中的一些人来说可能是显而易见的,但我却难以理解。有没有人愿意给我一个基本的 运行 下来?我是否正确设置了我的代码以获得我想要的结果?这是我目前的设置:
class Invite < ActiveRecord::Base
belongs_to :attending_guest, :class_name => "User"
belongs_to :attending_event, :class_name => "Event"
end
class User < ActiveRecord::Base
has_many :created_events, :foreign_key => "creator_id", :class_name => "Event"
has_many :invites, :foreign_key => :attending_guest_id
has_many :attended_events, through: :invites, source: :attending_event
end
class Event < ActiveRecord::Base
belongs_to :creator, :class_name => "User"
has_many :invites, :foreign_key => :attending_event_id
has_many :attendees, :through => :invites, :source => :attending_guest
end
基本上,一个事件有一个创建者,我想我已经正确地完成了这一部分。接下来,我希望能够获得参加该活动的用户列表。另外,我希望能够看到用户将要参加的活动。但是我什至如何建立一个邀请并让一个事件与一群用户相关联,rails 如何处理这个?如果有人能解释一下我如何做这些事情并给我一些 clarifications/tips,我将不胜感激。谢谢!
你能单独创建每个 ActiveModel 的实例并保存吗?确保你的外键是正确的。
如果关系等是正确的,我相信(伪)代码应该是这样的:
creator = User.where(...
event = Event.new
event.save! # You need to save before adding to association
user = User.where(...
user.invites.create!(creator_id: creator)
除非我遗漏了什么,否则应该大致如此。不过,请务必将其包装在交易中。
更新:我认为你应该可以在没有交易的情况下做到这一点:
creator = User.where(...
event = Event.new
user = User.where(...
user.invites.build(creator_id: creator)
event.save!
这应该有效并且更好,因为它允许您在 Event 中使用验证器来检查是否存在所需的关联。
使用 belongs_to
、has_many
等为您提供了一些非常方便的方法来将对象相互关联起来。例如 belongs_to
附带:
1. association
2. association=(associate)
3. build_association(attributes = {})
4. create_association(attributes = {})
5. create_association!(attributes = {})
到目前为止,你的问题是如何构建邀请并将它们与事件和用户相关联,这是我的建议:
由于在模型 Invite
中您使用 belongs_to
定义了 2 个关联,您可以使用上面列表中的方法 No.5
如下:
invite = Invite.create!
invite.create_attending_guest!(<the necessary attributes and respective values for creating a new user>)
invite.create_attending_event!(<the necessary attributes and respective values for creating a new event>)
或反过来:
guest = User.create!(<attrs>)
event = Event.create!(<attrs>)
invite = Invite.create!(attending_guest: guest, attending_event: event)
I want to be able to see what events a user is going to
您可以像这样访问它们:
u = User.find(5)
events = u.attended_events
how do I have one event be associated with a bunch of users
在这种情况下,您可以使用 has_many
添加的方法 <<
(其他请参见 here):
u1 = User.create!(<attrs>)
u2 = User.create!(<attrs>)
u3 = User.create!(<attrs>)
event = Event.create!(<attrs>)
event.attendees << u1
event.attendees << u2
event.attendees << u3