Delete specific table entries.

I am having the hardest time finding out how to manually modify or edit table entries. I keep finding ways to delete full columns of tables, and how to destroy the whole table, but what about wanting to delete specific users and listings in our tables? For example, what if I wanted to delete user id's 2, 3, and 5 in our user table? What will be the best way to do this? Thank You!

Posted over 5 years ago by Anthony Myers
Posted over 5 years ago by Anthony Myers

Oops, I finally found it! http://guides.rubyonrails.org/active_record_basics.html "user = User.find_by(id: '2')" and then " user.destroy".

0
Posted over 5 years ago by Alex Yang

Yes, you'll need to run these commands in your Rails console, which you can access from your command line by running 'rails console'. There's a lot more cool stuff you can do other than delete/destroy table entries - you should read up on the Rails console some more!

0
Posted over 5 years ago by joseph

where exactly is a good resource to go and learn the more advanced functions you can run in the rails console you would say?

0
Posted over 5 years ago by Alex Yang

Unfortunately, there isn't one good resource for this. This is because you can run any Ruby commands in the Rails console - it isn't like command-line commands where there is only a limited set of commands you can run.

As far as what I find useful in the Rails console, I most often use it to modify objects. For example, say I need to edit a user's account info. Then I would open up my Rails console, and run:

user = User.find(123) (123 is the ID of the user I'm looking for)
user.email = NEW_EMAIL@GMAIL.COM (NEW_EMAIL@GMAIL.COM is the new email address I want to set)
user.save

But there's a lot more you can do with the Rails console. For example, I could write a few lines of code to automatically reset the passwords of all users at once:

users = User.all
users.each do |user|
user.password = NEW_PASSWORD (NEW_PASSWORD is the reset password I want to use)
user.save
end

Of course, there are tons of other combinations you could do, but I hope this simple example helps you make sense of the possibilities.

0
Posted over 5 years ago by joseph

Yes great example is a good idea to look at traffic through the logs or mostly sued a third party analytic tool?

0
Posted over 5 years ago by Alex Yang

I'll use the logs for debugging purposes, either through 'heroku logs' or through a third-party tool like Loggly.

However, this is pretty inefficient for informational or analytic purposes. For that, I suggest looking into services like Mixpanel, Woopra, or Google Analytics. There are still other services that are more targeted toward real-time data. I've been thinking about putting together an analytics course in the future with material on setting up these services, thinking about the metrics you'll need to track, and drawing insights on user behavior. Is that something you would be interested in?

0
Posted over 5 years ago by joseph

Yes that would be very interesting if you show it in use for an existent project not just as abstraction but get into the details and real world examples of it's use! and a screencast on all the more advance function you could do in a console once your app is in production would be cool as well..

0
Posted over 5 years ago by Alex Yang

Great - I'll add both of those to my list!

0
Posted over 5 years ago by joseph

and in the analytic clips way to see the amount of current user register on the platform and basically analytics relating a user driven we app.

0