Skip to content

Instantly share code, notes, and snippets.

@waseem
Created February 1, 2011 08:04
Show Gist options
  • Save waseem/805567 to your computer and use it in GitHub Desktop.
Save waseem/805567 to your computer and use it in GitHub Desktop.
When you want to create an object and save it in one fell swoop, you can use the create method. Use it now to create another article:
>> Article.create(:title => "Some Fancy Title")
=> #<Article id: 4, title: "Some Fancy Title" >
Instead of returning true or false, the create method returns the object it created - in this case, an Article object. You're actually passing a hash of attributes to the create method. Although hashes are normally surrounded by curly braces, when a hash is only argument to a Ruby method, the braces are optional. You can just as easily create the attributes hash first and then give that to create:
>> attributes = { :titile => "Some other Fancy Title }
>> Article.create(attributes)
=> #<Article id: 5, title: "Some other Fancy Title" >
--
So the question is: what I should do to recieve "Article id: 5" from just hash. Notice that there is some empty stroke after hash creation, maybe it's a mistake?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment