Rails 6 belongs_to 块模型更新
Rails 6 belongs_to block model update
我有型号:
class User < ApplicationRecord
has_one :profile, :dependent => :destroy
before_create :init_setting
before_create :init_profile
after_create :add_gender_to_profile // this stop working
after_create :add_username_to_profile // this stop working
private
def init_profile
build_profile
end
def init_setting
build_setting
end
end
第二个模型当然是Profile
:
class Profile < ApplicationRecord
extend FriendlyId
friendly_id :username, use: :slugged
belongs_to :user
belongs_to :state
belongs_to :city
end
我认为配置文件的代码应该足够了。
还有模型:Country、State、City
class Country < ApplicationRecord
has_many :profiles
end
class State < ApplicationRecord
has_many :cities
has_one :country
has_many :profiles
end
class City < ApplicationRecord
belongs_to :state
has_many :profiles
end
现在...一切正常 add_gender_to_profile
- 正常,add_username_to_profile
也正常,但是...
仅当我从 Profile 模型关联中退出时才有效
belongs_to :state
belongs_to :city
如果我不扔掉它,我会在日志中看到类似这样的内容:
Profile Exists? (3.1ms) SELECT 1 AS one FROM `profiles`
WHERE `profiles`.`id` != 42 AND `profiles`.`slug` = 'majkel' LIMIT 1
↳ app/models/user.rb:66:in `add_username_to_profile'
user.rb
中的第 66 行
def add_username_to_profile
self.profile.update(:username => self.username)
end
此创建的配置文件有 id == 42
并且应该有 slug == majkel
问题出在哪里?为什么如果我取消注释 belongs_to: state
,并且 belongs_to :city
对于配置文件模型一切正常?
确定发现问题,但我还不明白:
translation missing:
pl.activerecord.errors.models.profile.attributes.state.required
由于 Rails 5,belongs_to
需要存在关联的父对象,否则在您保存时将无法通过验证。在您的代码中,您正在调用 build_profile
来初始化一个新的 Profile
,但此时您似乎没有创建关联的 State
对象。
一种选择是在创建时完全配置关联对象。另一种选择是将关联定义为可选:
belongs_to :state, optional: true
我有型号:
class User < ApplicationRecord
has_one :profile, :dependent => :destroy
before_create :init_setting
before_create :init_profile
after_create :add_gender_to_profile // this stop working
after_create :add_username_to_profile // this stop working
private
def init_profile
build_profile
end
def init_setting
build_setting
end
end
第二个模型当然是Profile
:
class Profile < ApplicationRecord
extend FriendlyId
friendly_id :username, use: :slugged
belongs_to :user
belongs_to :state
belongs_to :city
end
我认为配置文件的代码应该足够了。
还有模型:Country、State、City
class Country < ApplicationRecord
has_many :profiles
end
class State < ApplicationRecord
has_many :cities
has_one :country
has_many :profiles
end
class City < ApplicationRecord
belongs_to :state
has_many :profiles
end
现在...一切正常 add_gender_to_profile
- 正常,add_username_to_profile
也正常,但是...
仅当我从 Profile 模型关联中退出时才有效
belongs_to :state
belongs_to :city
如果我不扔掉它,我会在日志中看到类似这样的内容:
Profile Exists? (3.1ms) SELECT 1 AS one FROM `profiles`
WHERE `profiles`.`id` != 42 AND `profiles`.`slug` = 'majkel' LIMIT 1
↳ app/models/user.rb:66:in `add_username_to_profile'
user.rb
中的第 66 行def add_username_to_profile
self.profile.update(:username => self.username)
end
此创建的配置文件有 id == 42
并且应该有 slug == majkel
问题出在哪里?为什么如果我取消注释 belongs_to: state
,并且 belongs_to :city
对于配置文件模型一切正常?
确定发现问题,但我还不明白:
translation missing:
pl.activerecord.errors.models.profile.attributes.state.required
由于 Rails 5,belongs_to
需要存在关联的父对象,否则在您保存时将无法通过验证。在您的代码中,您正在调用 build_profile
来初始化一个新的 Profile
,但此时您似乎没有创建关联的 State
对象。
一种选择是在创建时完全配置关联对象。另一种选择是将关联定义为可选:
belongs_to :state, optional: true