我有一个基本has_many:通过关系是双向的:

calendars have many calendar_calendar_events
calendars have many events through calendar_calendar_events


events have many calendar_calendar_events
events have many calendars through calendar_calendar_events

我想要分配日历的事件与基本 calendar_ids= 功能,has_many:通过集起来,然而,我想重写这个功能增加一些额外的魔法。我已经看过的轨源和能找不到对此的功能。我想知道如果有人能告诉我到它。然后我会复盖,它对于此类添加的东西,我想:)

有帮助吗?

解决方案

你可以找到源代码的文件 lib/active_record/associations.rb 在线1295

    def collection_accessor_methods(reflection, association_proxy_class, writer = true)
      collection_reader_method(reflection, association_proxy_class)

      if writer
        define_method("#{reflection.name}=") do |new_value|
          # Loads proxy class instance (defined in collection_reader_method) if not already loaded
          association = send(reflection.name)
          association.replace(new_value)
          association
        end

        define_method("#{reflection.name.to_s.singularize}_ids=") do |new_value|
          ids = (new_value || []).reject { |nid| nid.blank? }
          send("#{reflection.name}=", reflection.class_name.constantize.find(ids))
        end
      end
    end

你绝对应该避免复盖这样这种方法来加魔法的东西。铁轨已经是"太多的魔法"的时候。我会建议创建一个虚拟的属性与你所有的定义逻辑的几个原因:

  1. 一些其他轨道方法可能会依赖默认执行
  2. 你依赖于一个具体API可能会改变未来版本的Email

其他提示

在一个位寻线我发现它的:

http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/collection_accessor_methods

它看起来并不像我想象会是什么样子,所以这就是为什么我可能错过了。我结束了重写日历=方法代替calendar_ids =方法和一切正常。

在响应于上述的答案,我用alias_method_chain覆盖默认设置器,并添加我的特征。工作得很好,虽然我不知道为什么我要送方法,而不是制定者只是用它正常的。它似乎没有工作,虽然如此,这将做到:)

  def calendars_with_primary_calendar=(new_calendars)
    new_calendars << calendar unless new_record?
    send('calendars_without_primary_calendar=', new_calendars) # Not sure why we have to call it this way
  end

  alias_method_chain :calendars=, :primary_calendar
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top