0

Here is my migration

create_table :customers do |t|
  t.string "name", :limit => 25 , :null => false
  t.string "email", :limit => 25, :null => false
  t.string "question", :limit => 255 , :null => true
  t.integer "status", :limit => 1, :default => 0, :null => false
  t.timestamps
end

Now, in the model, I want insert a value using this code:

Customers.new(:name => name, :email => email, :question => question, :status => 1)

But value does not inserted in table, why?

2 Answers 2

2

Because you haven't actually saved it.

customer = Customer.new(:name => name, :email => email, :question => question, :status => 1)
if custom.save
  // Saved succefully
else
  // Failed to save
end

Or just use create instead

customer = Customer.create!(:name => name, :email => email, :question => question, :status => 1)

With bang after create, it should raise an error if failed.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to insert in a line you need to use create method like below

Customers.create(:name => name, :email => email, :question => question, :status => 1)

Otherwise you need to create new object then need to use the save method to create a new record in DB, like below

customer = Customers.new(:name => name, :email => email, :question => question, :status => 1)
customer.save

If you need more clarification, let me know.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.