DEV Community

Vicente G. Reyes
Vicente G. Reyes

Posted on

Using the update_attribute() API using the rails console

I had a task at work to add data from the rails console after generating migrations and was puzzled on how to do it. After spending some time on the problem, I asked for help and this is what I got:

object=Model.first
object.description = "Lorem Ipsum"
object.save
Enter fullscreen mode Exit fullscreen mode

The problem was, it returned false which I had a hint that it didn't work.

I checked the frontend and it didn't show the data I entered and ran reload on the rails console again but it still didn't update the data.

I knew I had to check the docs and figure out how to do it.

I found this answer on SO which helped me with my problem.

object=Model.first
object.update_attribute(:book_description, "Lorem Ipsum")
object.save
Enter fullscreen mode Exit fullscreen mode

This updated the data on the frontend which was the goal of the task.

Top comments (0)