Dan Tappin
Dan Tappin

Reputation: 3032

Prevent update to an attribute?

I have a column that I want to lock after creation i.e. no updates to that column.

First off I know I can lock it down through the form but anyone can pass it through the URL.

I know I could build some sort of check in the update method to compare before and after the update. I can also add some conditional to the permitted parameters.

I was hoping that it could be a simple addition to the model. The more simple the better.

Upvotes: 0

Views: 411

Answers (1)

weilandia
weilandia

Reputation: 577

You can use attr_readonly:

class User < ActiveRecord::Base
  attr_readonly :your_field_name
end

Upvotes: 4

Related Questions