1
class Post < ActiveRecord::Base
post.has_many :comments, :as => :commentable
def method_missing(name, *args, &block)
puts "blah!"
end
end
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
然後在控制檯:method_missing在多態關聯中使用時不起作用?
>comment.commentable
=> #<Post id: 1022, title: "something">
>comment.commentable.class
=> Post(id: integer, title: string)
>comment.commentable.blah
NoMethodError: undefined method `blah' for "#<Post:0x10f1e9e10>":Post
from /Users/patrick/.rvm/gems/[email protected]/gems/activerecord-3.0.7/lib/active_record/associations/association_proxy.rb:216:in `method_missing'
>Post.find(comment.commentable).blah
=> "blah"
爲什麼這個不行?
嗯,在我的作品爲3.1。我的第一個假設是代理類的問題。 –