méthode non pour ActiveRecord::Relation

Utilisateur modèle

class User < ActiveRecord::Base
  has_many :medicalhistory 
end

Mdedicalhistory modèle

class Medicalhistory < ActiveRecord::Base
  belongs_to :user #foreign key -> user_id
  accepts_nested_attributes_for :user
end

Erreur

undefined method `lastname' for #<ActiveRecord::Relation:0xb6ad89d0>


#this works
@medicalhistory = Medicalhistory.find(current_user.id) 
print   "\n" + @medicalhistory.lastname

#this doesn't!
@medicalhistory = Medicalhistory.where("user_id = ?", current_user.id)
print   "\n" + @medicalhistory.lastname #error on this line
  • ce message d'erreur?
  • Ne @medicalhistory.first.lastname travail?
  • 🙁 oui, c'est....insight?
  • vous ne savez pas si c'est les "meilleures pratiques", mais @medicalhistory = Medicalhistory.où("user_id = ?", current_user.id)[0] devrait fonctionner
InformationsquelleAutor Omnipresent | 2011-05-14