Activerecord enum不会生成助手

时间:2014-12-26 15:12:30

标签: ruby-on-rails activerecord enums

我创建了一个活动记录枚举字段:role。将其添加到模型和表格中。但它不会产生助手吗?

会员资格模式

  enum role: [:employee, :admin, :owner]

  after_initialize :set_default_membership_role, :if => :new_record?

  def set_default_membership_role
    self.role ||= :employee
  end

pry输出

=> [#<Membership id: 1, company_id: 1, user_id: 1, created_at: "2014-11-25 16:06:03", updated_at: "2014-11-25 16:06:03", role: 0>]
[3] pry(main)> @membership.role
from /Users/gtheys/.rbenv/versions/2.1.2/gemsets/worke_rs/gems/activerecord-4.1.5/lib/active_record/relation/delegation.rb:136:in `method_missing'

是因为会员资格是用户和公司的联接模式吗?

模型/ User.rb

has_many :companies, :through => :memberships, dependent: :destroy

模型/ Companies.rb

has_many :users, :through => :memberships

或者还有另一个问题,为什么没有创建枚举助手?

1 个答案:

答案 0 :(得分:0)

您确定在实际会员模型上呼叫.role吗? 在您的示例(pry输出)中,@membership似乎是active_record/relation

尝试@membership.first.role