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 Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Editing Todo Lists

Nikolay Batrakov
Nikolay Batrakov
9,604 Points

TodoList.create() and create_todo_list I define - is these the same?

Well the define method subject is interesting and I found a lot of new from there but just for the purpose of clarification - is this ultimately the same stuff?

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Yes and no. TodoList.create() is a console way of creating a new object that belongs to TodoList model. You can create objects this way through the console, without starting the Rails server. This creates the object in memory and saves it into the database at the same time. A variant of this is TodoList.create!() which "raises exception if the creation fails" (for example if you have some validations that didn't pass). Another way of doing this is creating the object in the memory through TodoList.new() and then using the .save method on it, which will put it in the database (two lines instead of one).

create_todo_list that is defined in your spec creates the new object through the user interface - it runs the Rails server, goes to the appropriate page, fills the form and clicks the create button (Capybara simulates a real user - imagine a huge rodent sitting behind the keyboard and doing automated tests for you ;) ). When it clicks the Create Todo List button, under the hood it does run the .create method and passes the attributes from the form, so ultimately is is kinda the same thing, just takes input from different places. But the purpose of this whole method is to see if it works through the website - while running TodoList.create() can be done even if you have no forms and no HTML at all. There is a special kind of spec - controller spec - which would use the TodoList.create() to see if the method itself works properly and would not use the interface. Hope this helps.

Nikolay Batrakov
Nikolay Batrakov
9,604 Points

Now I see. The ways of input are different and we need to emulate the user totally. Thank you!