楷模
class User < ActiveRecord::Base
has_one :meta, class_name: SeoMetum, as: :metumable, dependent: :destroy
delegate :browser_title, :browser_title=, :meta_description, :meta_description=, to: :meta
has_one :collection
accepts_nested_attributes_for :collection
after_save :save_meta_tags!
def save_meta_tags!
meta.save
end
end
class Collection < ActiveRecord::Base
has_one :meta, class_name: SeoMetum, as: :metumable, dependent: :destroy
delegate :browser_title, :browser_title=, :meta_description, :meta_description=, to: :meta
belongs_to :user
after_save :save_meta_tags!
def save_meta_tags!
meta.save
end
end
class SeoMetum < ActiveRecord::Base
attr_accessible :metumable, :browser_title, :meta_description, :meta_keywords, :meta_author
belongs_to :metumable, polymorphic: true
end
这是有效的:
it 'meta_description' do
user.meta_description = 'This is my description of the user for search results.'
user.meta_description.should == 'This is my description of the user for search results.'
end
it 'meta_description' do
the_collection = user.collection
the_collection.meta_description = 'This is my description of the user for search results.'
the_collection.meta_description.should == 'This is my description of the user for search results.'
end
这是行不通的
it 'meta_description' do
user.collection.meta_description = 'This is my description of the user for search results.'
user.collection.meta_description.should == 'This is my description of the user for search results.'
end
# => nil
我的问题是为什么委派属性不会在嵌套属性中更新。我应该从哪里开始解决这个问题?谢谢。