Datamapper 덕분에 기존 데이터에 액세스하려고했습니다. 하지만 작동하지 않습니다. .first
, .all
과 같은 모든 메소드는 아무 것도 반환하지 않습니다. 나는 데이터베이스가 비어 있지 확신 ...왜 datamapper 메서드가 작동하지 않습니까?
을 undefined method update for nil:NilClass (NoMethodError)
반환 .update
때문에이 코드 리턴 아니라 new_consultation.date
new_consultation = Consultation.new
new_consultation.name = name_prestation
new_consultation.expense = expense_prestation
new_consultation.date = date_prestation.empty? ? Time.now.strftime("%d/%m/%Y") : date_prestation
new_consultation.save
puts new_consultation
puts new_consultation.date
이 전체 코드입니다 :
DataMapper.setup(:default, 'sqlite:medical_expenses.db')
class Consultation
include DataMapper::Resource
property :id, Serial
property :name, Text, required: true
property :expense, Text, required: true
property :date, Text, required: true
property :refund, Text, required: true
end
DataMapper.finalize()
DataMapper.auto_upgrade!()
# Enter and save new consultation
puts 'Prestations ?'
name_prestation = gets.chomp
puts 'Montant ?'
expense_prestation = gets.chomp
puts 'Date ? (par défaut date du jour)'
date_prestation = gets.chomp
print "Dépense enregistrée"
new_consultation = Consultation.new
new_consultation.name = name_prestation
new_consultation.expense = expense_prestation
new_consultation.date = date_prestation.empty? ? Time.now.strftime("%d/%m/%Y") : date_prestation
new_consultation.save
puts new_consultation
puts new_consultation.date
Consultation.first.update(refund: 'none')
puts Consultation.first
답장을 위해 @eiko에게 감사드립니다. 실제로 나는 환불이 요구되기를 원하지 않는다. 그러나 나는'required : true'를 제거하려고 시도하는데, 나는 "NOT NULL 제약 조건이 실패했습니다 : consultations.refund (DataObjects :: IntegrityError)"라는 에러 메시지가 나타납니다. 테스트하기 위해 나는'refund' 속성을'require : true'로 설정하고 그것을 설정합니다 ('new_consultation.refund = "no"'). 그러면 작동 중입니다. 하지만이 오류가 발생하지 않고'require : true'를 제거 할 수 있습니다. 어떻게 해결할 수 있습니까? – Orsay
@Orsay 구체적으로'require : false'를 넣어보십시오. – eiko
'require : false'를 넣고 설정 ('consultation.refund = 'no refund')하면 작동합니다. 하지만 그것을 설정하지 않으면'NOT NULL 제약 조건이 실패했습니다 : consultations.refund (DataObjects :: IntegrityError)' – Orsay