1

モデル

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

私の質問は、委任された属性がネストされた属性内で更新されないのはなぜですか。これを理解するには、どこから始めればよいでしょうか。ありがとう。

4

0 に答える 0