ruby-on-rails – Rails委托更新调用

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails委托更新调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个型号:
用户(电子邮件:字符串)
档案(姓名:字符串)
  1. class User < ActiveRecord::Base
  2. has_one :profile
  3. delegate :name,:name=,:to => :profile
  4. end
  1. class Profile < ActiveRecord::Base
  2. belongs_to :user
  3. end

铁轨

  1. u = User.new
  2. u.build_profile #=> init Profile
  3. u.name = 'foo'
  4. u.email = 'some@ema.il'
  5. u.save #=> both User and Profile are saved
  6.  
  7. u.name = 'bar'
  8. u.save #=> true,but changes in Profile were not saved!
  9.  
  10. u.email = 'new@ema.il'
  11. u.save #=> true,new User email was saved,Profile - still not!
  12.  
  13. u.name #=> 'bar',but in database it's 'foo'

为什么未更新配置文件(仅首次保存)?如何解决这个问题?

解决方法

ArcaneRain,您应该在关系中添加自动保存”选项,而不是为此添加回调:

has_one:profile,:autosave =>真正

你还应该调查’依赖’选项.
更多信息:
http://guides.rubyonrails.org/association_basics.html#has_one-association-reference

猜你在找的Ruby相关文章