【问题标题】:How to create through relations based on enum in join table?如何通过连接表中的枚举创建关系?
【发布时间】:2016-01-20 09:57:19
【问题描述】:

我想在 simple_form <%= f.association :organisators, collection: User.all %><%= f.association :helpers, collection: User.all %> 中选择它们的来源将是存储在具有适当枚举类型的参与连接表中的用户 ID,这是一种使用 ActiveRecord 关系自动完成的方法吗?

class Participation < ActiveRecord::Base
  belongs_to :user
  belongs_to :event

  enum kind: [:helper, :organisator]
end

class Event < ActiveRecord::Base
  belongs_to :user

  has_many :participations
  has_many :organisators, class_name: 'Participation'
  has_many :helpers, class_name: 'Participation'
end

class User < ActiveRecord::Base
  has_many :events
  has_many :participations
end

当我尝试保存当前版本时,它会出现:Couldn't find Participation with 'id'=1

event_params: {"helper_ids"=&gt;["2"], "organisator_ids"=&gt;["1"]}

【问题讨论】:

    标签: ruby-on-rails ruby enums relationships


    【解决方案1】:

    它们的来源将是存储在参与连接表中的用户 ID

    您需要从Participation 模型中提取,不是User 模型:

    class Participation < ActiveRecord::Base
      belongs_to :user
      belongs_to :event
    
      enum kind: [:helper, :organisator]
    end
    
    class Event < ActiveRecord::Base
      belongs_to :user
    
      has_many :participations
      has_many :organisators, -> { where kind: :organisator}, class_name: 'User', through: :participations, source: :user
      has_many :helpers,      -> { where kind: :helper}, class_name: "User", through: :participations, source: :user
    end
    
    class User < ActiveRecord::Base
      has_many :events
    
      has_many :participations
      has_many :participated_events, through: :participations
    end
    

    你基本上有一个has_many :through 关系。

    --

    因此,您应该能够使用:

    #app/views/events/update.html.erb
    <%= simple_form_for @event do |f| %>
       <%= f.association :organisators, collection: @event.organisators %>
       <%= f.association :helpers, collection: @event.helpers %>
       <%= f,submit %>
    <% end %>
    

    如果您提供一些关于您想要实现的目标的背景信息,将会有所帮助。上面的代码应该会有所帮助,我们可能需要稍微调整一下。

    【讨论】:

    • ActiveRecord::HasManyThroughSourceAssociationNotFoundError at /events/new Could not find the source association(s) "organisator" or :organisators in model Participation. Try 'has_many :organisators, :through =&gt; :participations, :source =&gt; &lt;name&gt;'. Is it one of user or event? 如果我尝试添加source: 'Participation' ,那么它会返回相同的错误。我想将现有用户分配给参与模型。
    • 所以您基本上只是想将新用户添加到活动中?
    • 是的,我想从列表中选择他们并指定为组织者或帮助者。
    • 好吧,让我写一个更新,你原来的问题很混乱
    【解决方案2】:

    我建议在枚举上使用单表继承。保留现有的 Participation 模型,并创建两个继承自它的其他模型(帮助者和组织者)。

    一旦您实现了这一点,您就可以拥有两种不同的对象类型,它们可以通过关系引用,同时共享一个数据库模型。

    看看这个文档: http://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html

    【讨论】:

    • 在这种方法中,当我尝试从多个选择框中选择现有用户时,它会返回错误Couldn't find ParticipationOrganizer with 'id'=5 [WHERE "participations"."type" IN ('ParticipationOrganizer')]。如何将现有用户分配给 event.participation_organizers?
    猜你喜欢
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多