ActiveAdmin无法识别模型中的“self”

时间:2015-09-03 02:19:25

标签: ruby-on-rails activeadmin

例如,当我GET /admin/consoles/1/edit时,会发生这种情况:

  

无法找到带有'id'=

的品牌

然后突出显示我所拥有的以下代码片段 /app/models/console.rb

def full_name
  brand = Brand.find(self.brand_id).name
  "#{brand} #{self.name}"
end

似乎无法识别self.brand_id。想法?

2 个答案:

答案 0 :(得分:1)

我需要看到你的app/models/console.rb确定,但似乎你应该有belongs_to关系然后你可以使用那种关系......就像这样:

class Console < ActiveRecord::Base
  belongs_to :brand

  def full_name
    "#{brand.name} #{name}"
  end
end

但也许你应该有这样的保护:

  def full_name
    ("#{brand.name} " if brand.present?) << "#{name}"
  end

答案 1 :(得分:0)

您可以通过测试是否存在brand_id参数来避免错误:

def full_name
  if self.brand_id.present?
     brand = Brand.find(self.brand_id).name
     "#{brand} #{self.name}"
  else
     self.name #or other combination of parameters not using the brand model
  end
end

如果这有助于您,请告诉我们。