Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.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 如何使用新方法扩展ActiveRecord关系?_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails 如何使用新方法扩展ActiveRecord关系?

Ruby on rails 如何使用新方法扩展ActiveRecord关系?,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,让我们想象一下,我有如下简单的ActiveRecord模型: class Post < ActiveRecord::Base belongs_to :category end class Category < ActiveRecord::Base has_many :posts has_many :published_posts, -> { where(:published => true) } end Reindexer.new(relation).re

让我们想象一下,我有如下简单的ActiveRecord模型:

class Post < ActiveRecord::Base
  belongs_to :category
end

class Category < ActiveRecord::Base
  has_many :posts
  has_many :published_posts, -> { where(:published => true) }
end
Reindexer.new(relation).reindex # how can I get relation here?
在这个方法中,我应该能够做如下事情:

class Post < ActiveRecord::Base
  belongs_to :category
end

class Category < ActiveRecord::Base
  has_many :posts
  has_many :published_posts, -> { where(:published => true) }
end
Reindexer.new(relation).reindex # how can I get relation here?

在RubyonRails中,正确的方法是什么?在这种情况下,如何访问当前关系?

我尝试将代码包含到动态添加到任何AR类中的ActiveRecord\u关系类中

如果您需要将.to_csv添加到所有调用,如MyModel.all.to_csv:

型号:

class MyModel < ActiveRecord::Base
  include ToCsv
end