Working with the Rails Console on Heroku
This guide walks through how to work with the Rails console on Heroku in order to update and create records in the production database. Additionally, we'll examine how to perform debugging on a feature on the live site.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

As promised in this guide we're going to take a look at how we can make changes to our live production data. We're going to update our user role and then we're also going to add in our skills right here. So let's come into the terminal and get clear and in instead of typing something like rails c here we're going to type

heroku run rails c

And that is going to open up a connection between our Heroku app and our database. And what we have here locally. So now we have access to the production environment and I can say

User.last

You can see that this is a user I created so I can say

User.last.update!(roles: "site_admin")

And it looks like that worked. So now if I come back I'm still logged in. So if I come back and now come to blogs you can see that now I have access to write a new blog. Looks like we have a little bit of a bug here

large

but I'm pretty sure that this has to deal with the fact that we don't we haven't created any topics yet. Let's come back really quick and let's create a topic and let's really quick just to verify. I'm going to open up our schema.rb and topics just has a title and also one other thing. I'm pretty sure that this is what the problem is but it's usually a good practice to actually look at the logs. So let's say

heroku logs -n 250

Sso we get back the last 250 lines. And my guess is we're going to see some type of a nil value here. So let's see what we have. So yes it looks like you can see right here we have a call to topic ID and here's the error it says completed 500 internal server error and then action view template error undefined ID for nil class. And yes I'm just about positive that it is going to be the blog topic ID. So let's come back and or I should say let's open up the production console so I'm going to say

heroku run rails c

And these are very common little things that you're going to run into when you have a production application. And so that's the reason why I'm keeping all of this in here and not editing it out because you'll most likely run into these kind of things as well. Some I'm going to say

Topic.create!(title: "Rails Development")

and let's do another one so I'm going to say

Topic.create!(title: "Dev Soft Skills")

These are a few that I wanted to have in there. I'll just do those two for right now and using ctl + d I can get out of the terminal and now let me hit refresh. Well it looks like we still have a little bug in where I think this is coming from is let's open up the code for that partial. Let's see this is going to be app/views/blogs/_form.html.erb. And you see where we're calling blog.topic.id (on line 16) for selected. I think that's where our bug is coming from in fact we saw that that's where it's coming from because when it said a nil class this means that it's actually looking for a blog that has a topic already. And when we run this it's throwing an error. So let me actually see what happens locally when we do this (Jordan deleted blog.topic.id from line 16. So let's start up the rails console.

rails s

or the rails server I should say and let's see what happens when we go and try to create a new blog locally. And this is a pattern that you're probably going to want to follow. I could have just made the change then pushed up to Heroku but that is a very slow way to test. Usually, it's better to test everything locally and then you can go and push everything up.

It says could not connect to server: No such file directory. Oh and it looks like I just need it. Yes it looks like I don't have postgres running so I'm going to open up my postgres app. At some spot it looks like that got turned off. So if you ever see an error like this that's probably the bug so I'm going to close this. Run it again. And now everything should be working. Ok, coming back, hit refresh. There we go. Now it's working. Let me log in as an admin user and we are in local host here so this should be what we want. So go into the blog now if I click and say Write a new blog. This is now. This is working it's not giving an error. Now let's see what happens if I click Edit here. So this is still working so I think that we didn't actually even need that call to the topic ID and that is what's throwing the error. So let's test this out and see if it works.

git status

we only changed that in one field

git add .

then

git commit -m "Removed topic id call for selected in blog form"

Let's push this up

git push

and the workflow here is to run git push and then

git push heroku master

Then this is going to push everything up to Heroku. One little thing just keep in mind we don't have to run rake db:migrate or heroku run rake db:migrate whenever we push up a change unless we made a change to the database if you did make a change to the database make sure that you run heroku run rake db:migrate in order to update that schema file. So this looks so far like everything is being pushed up. It's compressing all of our files it's going through that production mode if you remember when we were talking about the rails asset pipeline and how it minifies everything it's going through that process right now. So if we come back. Let's go to our site. Hit refresh and see if that fixed our bug for us. And yes it did. Perfect. OK so now we have the ability to create a blog and you can see that we have our topics so I'm going to say my test blog I'm going to give it. Rails Development the sample is going to be in draft mode. Just put some content here and actually let's also put some other content just to make sure. So if I hit submit everything there worked and you can see our syntax highlighting is working. Our slug's or working all of this is fantastic.

So that was one little issue. So we've updated that bug fix. And now let's come into the About Me section. Let's add some skills in here. So come to the terminal

heroku run rails c

And let's go and let's start adding some skills. I'm going to open up the schema.rb file again going to skills.
I always forget if I said percent_utilized or something like that so I'm going to grab that. And now as soon as a Heroku console is up we can go and create some skills so I'm going say

Skill.create!(title: "Ruby on Rails", percent_utilized: 45)

That looks like it work I can type

Skill.last

just to verify. And there we go nexts one let's go

Skill.create!(title: "JavaScript", percent_utilized: 20)

So far we have 65 total and now we can go 10 so this will take us to 75 for front end development

Skill.create!(title: "Front End Development", percent_utilized: 10)

Now let's go with 15 which will take us up to 90. And this one's going to be for SQL database management.

Skill.create!(title: "SQL Database Management", percent_utilized: 15)

So if were at 90 we just have 10 percent left and this one's going to be for machine learning.

Skill.create!(title: "Machine Learning", percent_utilized: 10)

Obviously, yours will probably be a little bit different. But I just wanted to kind of take you through the whole process so you can see all of that. And now let's come back here. And now if I hit refresh we should see those all popping up and yes perfect. So we have Ruby on Rails, javascript, front-end development, sql database management and machine learning all of those listed there. Eventually, I'm going to update my job history and education. But for right now I think this is looking really good. I don't really have any other changes on that side so that's pretty exciting. So now you should have a pretty much fully functional application. The only thing that we have left to do is to implement action cable because if you come here and come to your test blog and try to type some of this in you're going to see that we have an error. And this is because we have not implemented what we need on the action cable side. So that's going to be the last thing that we do so I'm going to end this guide we have everything already pushed up to github so we're good on that.
And in the next guide I'm going to walk through all of the things we need to do in order to have our actual cable component working on Heroku.

Resources