如何在rails 3中自动生成相关的多态activerecord对象

前端之家收集整理的这篇文章主要介绍了如何在rails 3中自动生成相关的多态activerecord对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
class ItemSource < ActiveRecord::Base
  belongs_to :product,:polymorphic => true
end

class RandomProduct < ActiveRecord::Base
  has_one :item_source,:as => :product,:autosave => true,:dependent => :destroy
end

我想做的是打电话:

a = RandomProduct.find(1)
a.item_source

如果item_source尚不存在(= nil),则自动构建(build_item_source).

以前,我用alias_chain_method做了这个,但是在Rails 3中不支持.
哦,我也试过这个无济于事:

class RandomProduct < ActiveRecord::Base
  has_one :item_source,:dependent => :destroy

  module AutoBuildItemSource
    def item_source
      super || build_item_source
    end
  end  
  include AutoBuildItemSource
end

解决方法

在Rails 3中,alias_method_chain(和alias_method和alias)工作正常:
class User < ActiveRecord::Base
  has_one :profile,:inverse_of => :user

  # This works:
  # 
  #   def profile_with_build
  #     profile_without_build || build_profile
  #   end
  #   alias_method_chain :profile,:build
  #
  # But so does this:

  alias profile_without_build profile
  def profile
    profile_without_build || build_profile
  end
end

但是总有accept_nested_attributes_for作为替代,在设置profile_attributes时调用build.将它与委托(可选)结合使用,您不必担心记录是否存在:

class User < ActiveRecord::Base
  has_one :profile,:inverse_of => :user
  delegate :website,:to => :profile,:allow_nil => true
  accepts_nested_attributes_for :profile
end

User.new.profile # => nil
User.new.website # => nil
u = User.new :profile_attributes => { :website => "http://example.com" }
u.profile # => #<Profile id: nil,user_id: nil,website: "http://example.com"...>

如果始终创建关联,则不需要委派(但无论如何都可能有用).

(注意:我设置:inverse_of使Profile.validates_presence_of:用户工作并通常保存查询.)

猜你在找的Ruby相关文章