Lucas
Lucas

Reputation: 2886

Rails Model: How to make an attribute protected once it's created?

I was just wondering: How can i make an attribute "write once" ? To be more precise, I'm using Devise and I want the user to be able to register with this email but then, once it's done, I want this email locked.

I read that forms can easily be bypass, so I want to make sure my model does that.

Also, i'm using in one of my form that: <%= f.email_field :email, :id => "email", :disabled => "disabled" %>

Is there any risks that an user can modify his email after being registered?

Thanks for your answers!

Upvotes: 1

Views: 494

Answers (1)

Olivier L.
Olivier L.

Reputation: 2583

attr_readonly allows setting a value at creation time, then prevents modifying it on updates.

class MyModel < ActiveRecord::Base

  attr_readonly :email

end

Upvotes: 3

Related Questions