Ruby on rails 如何使用新方法扩展ActiveRecord关系?
让我们想象一下,我有如下简单的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
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