I want to create a new "AnswerStat" object and save it in the database. I am getting:
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 does not exist, it refers I guess to the primary key of the table answer_stats which is id. I do not explicitly declare this attribute of course. Why is Rails looking for this inexistent attribute? Code:
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
I am calling it from the rails console, or from any controller, using:
Answer.all.each do |a|
AnswerStat.add_answer(a)
end
The answer_stats table (sqlite3 using 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
Thanks in advance
it could come from the statement attribute, which refers to an associated model.I think it's looking for an answer_stat_id foreign key in the statements table.
You should either :
@answer_stat.build_statement(...)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With