static public Method

Base.create(attributes = {})

Create a new resource instance and request to the remote service that it be saved, making it equivalent to the following simultaneous calls:

ryan = Person.new(:first => 'ryan')
ryan.save

The newly created resource is returned. If a failure has occurred an exception will be raised (see save). If the resource is invalid and has not been saved then valid? will return false, while new? will still return true.

Examples

Person.create(:name => 'Jeremy', :email => 'myname@nospam.com', :enabled => true)
my_person = Person.find(:first)
my_person.email
# => myname@nospam.com

dhh = Person.create(:name => 'David', :email => 'dhh@nospam.com', :enabled => true)
dhh.valid?
# => true
dhh.new?
# => false

# We'll assume that there's a validation that requires the name attribute
that_guy = Person.create(:name => '', :email => 'thatguy@nospam.com', :enabled => true)
that_guy.valid?
# => false
that_guy.new?
# => true

Source Code

# File active_resource/base.rb, line 350
def create(attributes = {})
  returning(self.new(attributes)) { |res| res.save }        
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.