我想創建一個新的「AnswerStat」對象並將其保存在數據庫中。我得到︰Rails中的ActiveRecord :: MissingAttributeError(無法寫入未知屬性)
can't write unknown attribute `answer_stat_id'
app/models/answer_stat.rb:14:in `new'
app/models/answer_stat.rb:14:in `add_answer'
app/controllers/users_controller.rb:31:in `block in index'
app/controllers/users_controller.rb:30:in `each'
answer_stat_id不存在,它是指我猜到表的主鍵id是id的answer_stats。當然,我沒有明確聲明這個屬性。爲什麼Rails正在尋找這個不存在的屬性?代碼:
class AnswerStat < ActiveRecord::Base
attr_accessible :contact, :statement, :answer_sum, :answer_count, :variation
belongs_to :contact, :class_name => "Friend"
has_one :statement
validates_uniqueness_of :contact, :scope => :statement
def self.add_answer(answer)
if stat = AnswerStat.find_by_contact_id_and_statement_id(answer.contact.id, answer.statement.id)
...........do some stuff...........
else
@answer_stat = AnswerStat.new(:contact => answer.contact, :statement => answer.statement, :answer_count => 1, :answer_sum => answer.description, :variation => answer.description)
@answer_stat.save
end
end
我從鐵軌控制檯調用它,或者從任何控制器,使用:
Answer.all.each do |a|
AnswerStat.add_answer(a)
end
的answer_stats表(sqlite3的使用PRAGME table_info(answer_stats)):
0|id|INTEGER|1||1
1|contact_id|integer|0||0
2|answer_sum|integer|0||0
3|answer_count|integer|0||0
4|created_at|datetime|1||0
5|updated_at|datetime|1||0
6|statement_id|integer|0||0
7|variation|integer|0||0
在此先感謝
什麼的'AnswerStat'看起來像表?另外,哪一行會導致錯誤? – Romain