Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby on Rails 5 Basics Using the Rails Console Rails Console: Creating Model Objects

Theo VOGLIMACCI
Theo VOGLIMACCI
8,027 Points

Why is the post object automatically saved into posts ?

Hello,

when we create a new instance of Post (post = Post.new) and then we use save on it. Why is post automatically saved into posts? We never have to specify where we want to save it?

Thanks, Theo

caven xu
caven xu
Courses Plus Student 13,400 Points

Imagining every Class(such as Post) has a warehouse(database). When you call 'save', it just like you throw a new instance(of a Class) into that warehouse. @posts = Post.all means we have the '@posts' to represent all the post instances. @posts is just a variable name, it can be anything. We can use @post to represent all posts, then use @single_post to represent every post included in @post. But usually variable names that meet our natural language let us have more mental resource to handle other things.

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

By default, Rails saves new model objects to a database table whose name is based on the name of the model class. So if your model class is named Post, then Rails will automatically add records to the posts database table. (Rails can convert the singular "post" to the plural "posts" all by itself.)

You can learn more about these defaults in this blog post.