Rails nested delegate attributes not being updated

大憨熊 提交于 2019-12-11 10:57:28

问题


Models

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

Here's what works:

  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

Here's what doesn't work

  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

My question is why doesn't the delegated attributes get updated within a nested attribute. Where should I start looking to figure this out? Thanks.

来源:https://stackoverflow.com/questions/12011941/rails-nested-delegate-attributes-not-being-updated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!