Ruby on rails 5 rails中Virtus的默认值

Ruby on rails 5 rails中Virtus的默认值,ruby-on-rails-5,virtus,Ruby On Rails 5,Virtus,我正在使用virtus(1.0.5)和rails(5.0.2)。我使用Virtus作为模型,因为它基于所访问的页面进行验证 我的组织模式如下 class Organization < ApplicationRecord validates :name, presence: true end 不幸的是,上面的代码不起作用 Loading development environment (Rails 5.0.2) 2.3.0 :001 > of = OrganizationProf

我正在使用virtus(1.0.5)和rails(5.0.2)。我使用Virtus作为模型,因为它基于所访问的页面进行验证

我的组织模式如下

class Organization < ApplicationRecord
  validates :name, presence: true
end
不幸的是,上面的代码不起作用

Loading development environment (Rails 5.0.2)
2.3.0 :001 > of = OrganizationProfileForm.new(Organization.last)
Organization Load (0.6ms)  SELECT  "organizations".* FROM "organizations" ORDER BY "organizations"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> #<OrganizationProfileForm:0x007f9d61edd6a8 @organization=# <Organization id: 1, name: "Some name", call_center_number: "4892374928", created_at: "2017-03-29 09:35:22", updated_at: "2017-03-29 09:37:59">>
2.3.0 :002 > of.call_center_number
=> nil
加载开发环境(Rails 5.0.2)
2.3.0:001>of=OrganizationProfileForm.new(Organization.last)
组织加载(0.6ms)选择“组织”。*从“组织”中按“组织”排序。“id”描述限制$1[[“限制”,1]]
=> #
2.3.0:002>呼叫中心号码
=>零

尝试删除行
private
,因为这可能会阻止对方法
组织呼叫中心号码的调用。上的默认值示例使用公共实例方法,而不是私有实例方法

请参阅:

我们需要在initialize方法中调用super()

Loading development environment (Rails 5.0.2)
2.3.0 :001 > of = OrganizationProfileForm.new(Organization.last)
Organization Load (0.6ms)  SELECT  "organizations".* FROM "organizations" ORDER BY "organizations"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> #<OrganizationProfileForm:0x007f9d61edd6a8 @organization=# <Organization id: 1, name: "Some name", call_center_number: "4892374928", created_at: "2017-03-29 09:35:22", updated_at: "2017-03-29 09:37:59">>
2.3.0 :002 > of.call_center_number
=> nil