Hey All, Two questions. Firstly, what is the difference between create and create! I see that create is document in the api api.rubyonrails.org/classes/ActiveRecord/Base.html#M002269 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Aug 19, 10:00 am, James Englert <englert.ja...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey All, > > Two questions. > > Firstly, what is the difference between create and create! > > I see that create is document in the api > > api.rubyonrails.org/classes/ActiveRecord/Base.html#M002269 > > 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! > -Jimcreate! is here: api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html I find railsapi.com to be the best tool for browsing the Rails documentation.
James Englert 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. -- Posted via ruby-forum.com.