A
A
Anton2016-08-14 01:49:47
Ruby on Rails
Anton, 2016-08-14 01:49:47

Why is the record not being added to the table?

I'm trying to add a record to a table.
First, I create a request:

result = model.build(
  # Тут поля...
)

Next I do:
if result.save
  puts 'Да'
else
  puts 'Нет'
end

Before I savewrote:
puts result.to_json
And this brought out a line in which it is clear that all required (NOT NULL) fields are filled in, ID is still null. All right.
But when it comes to save, it prints "No".
What could be the problem? Is there any way to get the text of the error?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Beloshitsky, 2016-08-14
@mbeloshitsky

Is there any way to get the text of the error?
Usually the error texts are inresult.errors.messages

A
Anton Dyachuk, 2016-08-16
@Renius

Firstly

puts 'Нет'
puts result.errors.messages

Secondly, write tests, it's simple and you will have heavenly happiness on earth
valid_attributes = {foo: 1, bar: 2}
object = Object.create(valid_attributes)
expect(object).to be_valid

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question