Reputation: 319
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?
Upvotes: 0
Views: 95
Reputation: 4220
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.
Upvotes: 0
Reputation: 3080
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.
Upvotes: 2