加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 04:14:15 所属栏目:百科 来源:网络整理
导读:我有两个型号: 用户(电子邮件:字符串) 档案(姓名:字符串) class User ActiveRecord::Base has_one :profile delegate :name,:name=,:to = :profile end class Profile ActiveRecord::Base belongs_to :userend 铁轨 u = User.new u.build_profile #= init
我有两个型号:
用户(电子邮件:字符串)
档案(姓名:字符串)
class User < ActiveRecord::Base
   has_one :profile   
   delegate :name,:name=,:to => :profile   
end
class Profile < ActiveRecord::Base
  belongs_to :user
end

铁轨

u = User.new 
u.build_profile         #=> init Profile
u.name = 'foo'
u.email = 'some@ema.il'
u.save                  #=> both User and Profile are saved

u.name = 'bar'
u.save                  #=> true,but changes in Profile were not saved!

u.email = 'new@ema.il'
u.save                  #=> true,new User email was saved,Profile - still not!

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读