开发者

Received ActiveRecord Relation, but need object

i've written the following classes:

class Occupation < ActiveRecord::Base
  has_many :pref_labels
  has_many :cv_occupations
  has_many :cvs, :through => :cv_occupations
  validates_uniqueness_of :uri

  # This function will return the specified label for the given language.
  def label(language = Language.find_or_create_by_code(:en))
    self.pref_labels.where("language_id = #{language.id}")
  end
end

class PrefLabel < ActiveRecord::Base
  belongs_to :language
  belongs_to :concept
  belongs_to :skill
  belongs_to :occupation
  validates_uniqueness_of :value, :scope => [:language_id, :value]
  validates_uniqueness_of :language_id, :scope => [:language_id, :value]
end

In my view, I call the following: %td= occupation.label(@language) but this returns as error :

undefined method `value' for #<ActiveRecord::Relation:0x80c8368>

How can I get the actuall object returned instead o开发者_开发知识库f the relation? I know this has something to do with the lazy loading....


Change

self.pref_labels.where("language_id = #{language.id}")

To

self.pref_labels.where("language_id = #{language.id}").all #or .first if you only one the first one
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜