在 Rails 3.2 中,我试图在创建 aOdontologicUnity
时设置默认实例User
。
用户:
require "odontological_unity"
class User < ActiveRecord::Base
attr_accessible :odontologic_unity, ...
...
belongs_to :odontologic_unity
after_create :associate_default_unity
private
def associate_default_unity
ou = OdontologicUnity::DEFAULT
ou.save!
self.update_attribute :odontologic_unity, ou
end
end
牙科学统一:
# encoding: UTF-8
class OdontologicUnity < ActiveRecord::Base
...
DEFAULT = OdontologicUnity.new(
name: 'Actualiza el nombre',
address: 'Actualiza la dirección',
nit: 'Actualiza el NIT',
mission: "Actualiza la misión",
vision: "Actualiza la visión"
)
end
该代码可以正常工作而不会引发异常,但是当我查看我的数据库时,我得到了这个(odontological_unity_id: nil
):
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 9]]
=> #<User id: 9, email: "user2@example.com", password_digest: "$2a$10$BPDmM6stW0sO6tQbwwJCxuWO4hvWIAeSzogEl2GGiqmo...", remember_token: "ltU5RBcifDsr9uPIci9z_A", names: "Example User", lastnames: "Some Lastnames", identity_document_number: "1515", odontological_unity_id: nil, created_at: "2012-06-24 15:00:17", updated_at: "2012-06-24 15:02:05">
奇怪的是OdontologicUnity
记录实际上是创建的,但没有与用户相关联。