Is it possible to do an association extension on a belongs_to
relation?
I have tried to do it using a module but keep being told that the method I'm calling is private:
module TestExtension
def test
puts 'test successful'
end
end
class Question < ActiveRecord::Base
belongs_to :user, extend: TestExtension
end
Every time I run it though it complains that the method is private
q = Question.first
q.test
# => NoMethodError: Attempt to call private method开发者_JAVA百科 `test'
I'm not 100% clear whether it's possible to do AR Extensions on belongs_to. It was working fine on Rails 3.0.7 but is now failing in 3.1.0
This is a known issue in 3.1.0 that hasn't been resolved yet. Basically, the new association design in 3.1.0 doesn't support extending belongs_to
associations since it was never a supported feature in the first place. However, the issue is still open so it may be resolved in the future; you should probably comment on the issue to voice support if you want it.
Also, the private method error you're getting, you would be getting even if you didn't have the extend: TestExtension
part; I believe #test
is a private method on all ActiveRecord
objects.
精彩评论