现在我无法在这里找到一种在第1行和第2行之间生成回调的方法:
f = Foo.new
f.some_call
f.save!
Run Code Online (Sandbox Code Playgroud)
有没有办法模拟什么是有效的after_new回调?现在我正在使用after_initialize但是使用它有潜在的性能问题,因为它会触发很多不同的事件.
我有一个有很多孩子的简单父对象.我正在试图弄清楚如何使用命名范围来带回具有特定数量的孩子的父母.
这可能吗?
class Foo < ActiveRecord::Base
has_many :bars
named_scope :with_no_bars, ... # count of bars == 0
named_scope :with_one_bar, ... # count of bars == 1
named_scope :with_more_than_one_bar, ... # count of bars > 1
end
class Bar < ActiveRecord::Base
belongs_to :foo
end
Run Code Online (Sandbox Code Playgroud)
我希望能做类似的事情 Foo.with_one_bar
我可以在父类上编写类似这样的方法,但我宁愿拥有命名范围的强大功能