【问题标题】:Querying From :through Association查询自:通过关联
【发布时间】:2016-02-18 13:24:58
【问题描述】:

我正在创建一个 rails 应用程序来学习 has_many :through associations... 我知道我没有遵循 rails 约定,但那是因为我想了解 Active Record 必须提供的所有不同选项。

我的问题是,我认为我已经创建了关系,但我不知道如何建立邀请并查询它们以获取参加者和参加的活动。我必须先创建邀请吗?如果是这样,我如何将它与事件相关联?那么,如何才能让很多用户参与到活动中呢?这些问题对你们中的一些人来说可能很明显,但我很难理解它。有人会好心给我一个基本的运行吗?我是否正确设置了我的代码以获得我想要的结果?这是我到目前为止的设置:

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 是如何处理这个问题的?如果有人可以请解释我如何去做这些事情并给我一些澄清/提示,我将不胜感激。谢谢你!

【问题讨论】:

    标签: ruby-on-rails ruby activerecord associations


    【解决方案1】:

    使用belongs_tohas_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)
    

    我希望能够看到用户将要参加哪些活动

    您可以像这样访问它们:

    u = User.find(5)
    events = u.attended_events
    

    如何让一个事件与一群用户相关联

    在这种情况下,您可以使用has_many添加的方法&lt;&lt;(其他请参见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
    

    【讨论】:

    • 谢谢,这就是我要找的!
    【解决方案2】:

    您能否分别创建每个 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 中使用验证器来检查所需的关联是否存在。

    【讨论】:

    • 我得到了将创建者与活动相关联的部分,我现在要做的是构建邀请并与活动和用户相关联。
    • 这就是上面的代码应该做的。 #build 应该自动设置关联,它还应该处理其他模型的方法,包括 :through。这都是关于关系的,你从哪个“方面”添加它并不重要(参见@Zozo的回答,你可能更喜欢它)。
    猜你喜欢
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多