Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 这是已知的Rails管理器模式吗_Ruby On Rails - Fatal编程技术网

Ruby on rails 这是已知的Rails管理器模式吗

Ruby on rails 这是已知的Rails管理器模式吗,ruby-on-rails,Ruby On Rails,我正在浏览一个同事代码,但找不到一个使用过该代码的教程。有人能给我指出一些已经使用过的资源吗。这使得代码非常干净,但我没有找到任何引用。这只是这门课的一部分。它还包括其他一些方法 class Manager include ActiveModel::Model include ActiveModel::Associations attr_accessor :application_id, :user_id, :user_application_id,

我正在浏览一个同事代码,但找不到一个使用过该代码的教程。有人能给我指出一些已经使用过的资源吗。这使得代码非常干净,但我没有找到任何引用。这只是这门课的一部分。它还包括其他一些方法

class Manager
        include ActiveModel::Model
        include ActiveModel::Associations

      attr_accessor :application_id, :user_id, :user_application_id,.........
      belongs_to :application
      belongs_to :user_application
      belongs_to :user .. more belongs .......

       # This method is necessary to enable this ActiveModel Class to be used in views along with Form helpers
      def self._reflect_on_association(association) #:nodoc:
       _reflections[association.to_sym]
      end

      def []=(attr, value)
        self.send("#{attr}=", value)
      end

      def [](attr)
        multi_attribute_ids = [:some_ids.to_s, :someid2.to_s]
        return if multi_attribute_ids.include?(attr)
        self.send(attr)
      end
      def applicant_name 
      end
      -- some more methods
end

这样一个“经理”有什么用呢。在这里使用self.send的两种方法是什么。这是rails中的一种常见模式。

是的,随着ActiveModel在rails 3中的引入,使用域对象(在本例中称为管理器)已成为一种越来越常见的模式,这些对象不受实际数据库表的支持,但看起来和感觉上都像模型

尽管ActiveModel可以选择Rails模型特性合并到任意类中,但这种模式是Rails长期以来的先驱


正如您发布的示例中所清楚说明的那样,此模式允许我们定义虚拟模型和虚拟关联,这些模型和关联可以轻松利用表单帮助程序和假设模型对象编写的其他rails细节。

您的同事在哪里?
def[]=(attr,value)
def[](attr)
是括号设置器和获取器。当您执行
管理器[:foo]
管理器[:foo]='bar'
时,会调用它们。Ruby有很多看起来很奇怪的方法(比如
+
==
=
),因为大多数操作符都只是方法。是的..谢谢链接。。我以前见过这个。此代码不使用“在关联上反映”。。然而,我们不得不添加这个,我们的代码在没有添加这个的情况下在coffeescript中中断。