I’m using Ruby on Rails association to link the Store model to the Product model, using:
store has_many :products
product belongs_to :store
The only condition on the Product model, is the presence of a name:
validates :name, :presence => true
To create a new product, I use this code inside the Products controller, create method:
@store = Store.find_by_id session[:store_id]
if @store.products.create(:name => params[:name])
redirect_to :back, :notice => "New product successfully created."
else
redirect_to :back, :alert => "Can't create new product."
end
The problem, is that it works however the product name exists or not. I mean, in all cases, I have the “New product successfully created.” message, even if the product name is empty.
I can’t figure out where is the problem. Any help, please?
From documentation:
You should use another method to understand the saved record or not.