0

For some weird reason the new_messages_count column doesn't update to zero inside the database. I'm doing this from a controller:

 @conversation = Conversation.find(params[:id])
 @conversation.update(new_messages_count: 0)

The column is set like this: t.integer :new_messages_count, default: 0, null: false

If I do the above inside the rails console then the column gets updated to zero. I have no idea why this is happening, but can someone tell me how to update a column to zero from a controller?

Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
Dev
  • 437
  • 6
  • 25
  • have you whitelisted field in controller file? – Manoj Menon Mar 01 '21 at 10:18
  • also add your log while you submit request – Manoj Menon Mar 01 '21 at 10:19
  • You could always use a debugger and see if `@conversation` fails due to any validation checks. Check the logs too, if you see a Commit message or a Rollback happening. – Kedarnag Mukanahallipatna Mar 01 '21 at 10:31
  • Thanks for the reply @ManojMenon. I don't think that I have to whitelist the column inside the controller... because I have the above code inside a show method. So when I visit that show page the column `new_messages_count` needs to update to zero. Which is not happening! Also now that I'm double checking the console the update action is not apperaing at all. – Dev Mar 01 '21 at 10:32
  • `Conversation Update (0.8ms) UPDATE "conversations" SET "updated_at" = $1 WHERE "conversations"."id" = $2 [["updated_at", "2021-03-01 10:34:42.686009"], ["id", 3]] ↳ app/controllers/messages_controller.rb:11:in `chat_room'` (0.4ms) – Dev Mar 01 '21 at 10:36
  • Thanks for the reply @Kedarnag Mukanahallipatna. I'm getting a commit as you can see above – Dev Mar 01 '21 at 10:36
  • Could you try `@conversation.update_column(new_messages_count: 0)` ? If this works, then I think there is some validation that's failing the `@conversation.update` – Kedarnag Mukanahallipatna Mar 01 '21 at 10:39
  • How does your `Converstation` model look like? – Eyeslandic Mar 01 '21 at 13:44

2 Answers2

-1

You may have some validation issues

You can replace update by update_columns to update your column even if your instance is not valid. (or make sure that your instance is valid)

fabster
  • 1
  • 1
  • Thanks for the reply @fabster, I get this error `new_messages_count is marked as readonly` – Dev Mar 01 '21 at 10:42
  • @Dev oh your instance is read only. I'm not sure where does this come from but a solution could be : ``` conversation = Conversation.find(params[:id]) conversation.instance_variable_set(@readonly, false) conversation.update(new_messages_count: 0) ``` – fabster Mar 01 '21 at 10:54
  • getting this now: `nil is not a symbol nor a string` – Dev Mar 01 '21 at 10:58
  • ah, forget about my last answer then maybe try to add this in your model conversation.rb `def readonly? false end` – fabster Mar 01 '21 at 11:14
  • @Dev But then again, I'm not sure why your instance is in a readonly state in a first place. If it does not work I Hope someone else come and can tell us why ! – fabster Mar 01 '21 at 11:17
-1

Validation can block this update You can check with update_columns

 @conversation = Conversation.find(params[:id])
 @conversation.update_columns(new_messages_count: 0)
  • I'm getting this error with your code `new_messages_count is marked as readonly` – Dev Mar 01 '21 at 10:46
  • https://stackoverflow.com/questions/639171/what-is-causing-this-activerecordreadonlyrecord-error This may help you – usman azmat Mar 03 '21 at 12:24