有如下两个自定义的 model 类:
class Blog
...
def add_entry(entry)
@entries << entry
end
...
end
# app/models/post.rb
class Post
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_accessor :blog, :title, :body, :pubdate
def initialize(attrs = {})
attrs.each do |k,v| send("#{k}=",v) end
end
def publish(clock = DateTime)
self.pudate = clock.now
blog.add_entry(self) # <=疑惑点?
end
def persisted?
false
end
end
不太明白 Post 中的 blog 是如何初始化为一个 Blog 的实例的,请高手指点或给个思路,谢谢~