Q: When I use SomeClass.new, it creates an object, but it doesn’t save–even if I call save! What gives?
A: There’s something tricky going on here. Here’s an example of what we mean:
class SomeController < ApplicationController
def createNewFoo
@foo = Foo.new(@params[:foo])
# ...
@food.save
end
end
There are a couple of possibilities. The first thing to check is if @foo is nil. If it is, then your object is not being created properly. (Check your params; you need to make sure you have enough data there to create the new instance.)
The next possibility is if @foo IS defined--but not all of that properties. For example, @foo.bar and @foo.otherParam have values. In this case, you need to check if @foo.id is nil. If it is, it means @foo is failing the model validation.
In this case, double-check your model code to see what validation is in place, then take the appropriate steps to make sure @foo gets constructed properly. (Or, maybe you have nonsensical validation in place; in that case, remove the offending code.)