Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.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/ruby/23.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 ActiveRecord:在哪里定义方法?_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails Rails ActiveRecord:在哪里定义方法?

Ruby on rails Rails ActiveRecord:在哪里定义方法?,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我有一个关于Rails的ActiveRecord的问题 例如,我有一个服务模型,服务有名称作为一列 这是app/model/service.rb class Service < ActiveRecord::Base def self.class_method puts 'This is class method' end def instance_method puts 'This is instance method' end end 这很容易。但当

我有一个关于Rails的ActiveRecord的问题

例如,我有一个
服务
模型,
服务
名称
作为一列

这是
app/model/service.rb

class Service < ActiveRecord::Base

  def self.class_method
    puts 'This is class method'
  end

  def instance_method
    puts 'This is instance method'
  end
end
这很容易。但当我在数组中获取ActiveRecord实例时

Service.where(id: [1,2,3])
我需要这样的方法

Service.where(id: [1,2,3]).sample_method

def sample_method
  self.length
end
但如何以及在何处定义活动记录数组的方法?我希望像处理其他
服务
类或实例一样处理此对象


谢谢。

你的方法,如
get\u count
get\u name
有点毫无意义。。。为什么不干脆做:

Service.count

Service.find(1).name
类方法如
count
,实例方法如
name
(即数据库列名)都是公共的,因此您不需要定义自己的getter方法

对于第二个示例,您可以编写以下内容:

Service.where(id: [1,2,3]).map{ |s| s.name }
或相当于:

Service.where(id: [1,2,3]).map(&:name)
但是下面的方法实际上更有效,因为它是用SQL而不是ruby执行计算的。(如果您不明白我的意思,请尝试运行这两个版本,并在日志中比较生成的SQL):


首先,
其中
返回ActiveRecord::Relation对象,而不是数组。它的行为类似于数组,但继承了load more方法来处理数据/构造SQL以查询数据库

如果要向ActiveRecord::Relation类添加其他功能,可以执行以下操作:

class ActiveRecord::Relation 
  def your_method
    # do something
  end
end
这将需要驻留在有意义的地方,例如lib目录或config/initializer

这应该允许您执行以下操作

Service.where(id: [1,2,3]).your_method
您可以对任何Ruby类执行类似的操作,例如哈希类或数组类


然而,几乎总有比扩展/重写Rails/Ruby源代码类更好的解决方案…

对于这个模棱两可的问题,我很抱歉,我只是想知道如何以及在哪里为活动记录数组实例定义方法。为了避免误解,我对代码做了一点修改。我的答案仍然几乎没有改变。。。我将把它写成
Service.where(id:[1,2,3]).map(&:length)
。您也可以简单地将其定义为
def self.sample_method….
,但您在原始或更新的问题中都没有提供合理的用例。请也更新第一段。啊,我误解了。是,
其中
返回ActiveRecord::Relation对象,而不是数组。所以最好的方法是继承ActiveRecord::Relation类并添加实例方法。谢谢。你可以这样做是的,但是一般来说,向ActiveRecord::Relation类添加方法不是最好的方法。我个人会创建一个单独的类或模块,并将服务结果传递给它,其中(id:[1,2,3])。然后,您可以在该类中执行需要执行的操作,而无需更改ActiveRecord::Relation的行为。这是一种更好的实践,因为您的特定功能可以单独测试,并且不会有风险更改应用程序其他区域中关系类的库存行为。
class ActiveRecord::Relation 
  def your_method
    # do something
  end
end
Service.where(id: [1,2,3]).your_method