Create! Syntax

Hey All,

Two questions.

Firstly, what is the difference between create and create!

I see that create is document in the api

What is the difference between the two of these functions and where is
the
create! function defined.

Also,

What are the differences between these two calls?

a) SomeModel.create!{ :property_a => ‘value’, :property_b => ‘value 2’
}
b) SomeModel.create!({ :property_a => ‘value’, :property_b => ‘value
2’ })

Thanks a lot for the help!
-Jim

On Aug 19, 10:00 am, James E. [email protected] wrote:

What is the difference between the two of these functions and where is the
-Jim
create! is here:

http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html

I find http://railsapi.com to be the best tool for browsing the Rails
documentation.

James E. wrote:

Hey All,

Two questions.

Firstly, what is the difference between create and create!

create returns true if successful, false otherwise. create! throws an
exception if the operation fails for any reason.

What are the differences between these two calls?

a) SomeModel.create!{ :property_a => ‘value’, :property_b => ‘value 2’
}
b) SomeModel.create!({ :property_a => ‘value’, :property_b => ‘value
2’ })

Nothing. The {} indicating the hash is optional if it’s obvious to the
Ruby interpreter. If you were to have two hash arguments you need to
separate them properly so the interpreter understand what to do:

do_this({ :a => ‘a’, :b => ‘b’ }, { :c => ‘c’, :d => ‘d’ })

See if the braces aren’t present then it would look like:

do_this(:a => ‘a’, :b => ‘b’ , :c => ‘c’, :d => ‘d’)

Which is probably not what you want if you intended to send two hashes
to the method. In the second case there is only one hash send and the
second argument would not be set.

It might do you some good to go back and brush up your basic Ruby
skillz.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs