Forum: Ruby on Rails Errors object missing when saving AR in Web Services method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E1061e0939f6f821482b1ad9a2225bdb?d=identicon&s=25 Martin Sojka (msojka)
on 2005-12-22 14:17
Hello,

I'm not able to get Errors object when saving some Model instance in
ActionWebService::Base subclass.

I want to create API method that will return ID of the object when saved
successfully. When validation fails and the object is not saved I'd like
the method to return ID, as well as the array of ValidationError custom
struct. But I cannot fill in this because unsaved object contain NO
errors object, it just contains @new_record = true (and this indicates
that it has not been saved).

Here is the sample API method:

<code>
# Not all required attrs are provided since I want it to be invalid
def new_customer(first_name)
  customer = Customer.new( :first_name => first_name )
  customer.save
  customer
end
</code>

Return value after invoking this method (through web services scaffold)
shows this

<code>
#<Customer:0x367d680 @new_record=true,
@attributes={"first_name"=>"Martin", last_name"=>"", "created_at"=>nil,
"email"=>""}>
</code>

As you can see there is no errors object in the customer. However
@new_record is correctly set to true (last_name is required, and email
format is validated).

Do you have any idea why is the Errors object missing here?

Thanks for any help!
This topic is locked and can not be replied to.