Launching Rails console1:04 with Jay McGavren
We've seen how to use the Web interface to create new model objects. Now, I want to show you how to do the same from the terminal, using the "Rails console".
To launch the Rails console, type in your terminal:
You'll leave your system prompt, and be taken to a prompt provided by Ruby. Here you can type Ruby expressions. They'll be evaluated, and you'll be shown the result.
[MUSIC] 0:00 We've seen how to use the web interface to create new model objects. 0:04 Now I wanna show you how to do the same from the terminal using the Rails console. 0:08 The Rails console is useful when you need to do an operation on many model objects 0:12 at once, or to look at model attributes that you haven't yet added to your views. 0:16 We've seen the Rails server and generate sub commands already. 0:21 Console is another sub command. 0:25 So let's make sure the Rails server is stopped and 0:27 we're back at our system prompt. 0:29 Then we type bin/ rails console in our terminal. 0:31 You'll leave your system prompt and be taken to a prompt provided by Ruby. 0:37 Here you can type Ruby expressions. 0:40 They'll be evaluated and you'll be shown the result. 0:42 If you've used Ruby's IRB command before, it works just the same way. 0:45 The difference is you have your full Rails environment loaded, 0:49 including your model classes. 0:52 Your model class is connected to your database so 0:54 you can create, read, update, or delete model data. 0:56 Let's try out all the cred operations using our post model class. 1:00
You need to sign up for Treehouse in order to download course files.Sign up