Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit a database record through the Rails console

Example scenario: I'm on my mac's Terminal inside the Rails console and have just entered the following database record with a misspelling in one of the fields:

irb(main):019:0> Meme.create(:category => 'funny', :title => 'Balloon frihgtens cat')

Q: Using the Rails console, how can I fix that record to have the correct spelling of "frihgtens"?

like image 686
Stephen B. Thompson Avatar asked May 21 '15 03:05

Stephen B. Thompson


People also ask

How do I delete a record from a database in rails?

Rails Delete operation using delete method Unlike the destroy method, with delete, you can remove a record directly from the database. Any dependencies to other records in the model are not taken into account. The method delete only deletes that one row in the database and nothing else.

What is Activerecord in Ruby on Rails?

Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.


1 Answers

Step 1. Find the record.

Step 2. Edit the record.

Assuming title is unique, the following should work:

> m = Meme.where(:title => 'Balloon frihgtens cat').first
> m.title = 'Balloon frightens cat'
> m.save

Read up http://guides.rubyonrails.org/active_record_querying.html to learn more about using active record.

like image 142
Prakash Murthy Avatar answered Oct 08 '22 07:10

Prakash Murthy