Rails嵌套委托属性未更新

时间:2012-08-17 19:22:04

标签: ruby-on-rails ruby activerecord rspec

模型

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

我的问题是为什么委托属性不会在嵌套属性中更新。我应该从哪里开始考虑这个问题?感谢。

0 个答案:

没有答案