Forum: Ruby on Rails how to make field in model immutable after create

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
06e1b396407b733b4b3d825cb2c57856?d=identicon&s=25 Leo -- (leopardus)
on 2006-05-08 07:54
(Received via mailing list)
Hello everyone!

I want to have login field in User model which is set only once (on
create), and then it should fail validation if the value is changed.
How can I do this? I dig around for validation and tried

def after_validation_on_update
  unless User.find_by_login(:login)
    errors.add(:login, "is immutable. You can't change it")
  end
end

in model, but somewhat it doesn't work. Any hints on how can I make
field immutable after create?

Peace,
olegf
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-05-08 08:06
(Received via mailing list)
I'm almost positive there's a better way, but you could try something
like this in the validate method of your model.

def validate

    errors.add( :login, "Can't change your login details") if
self.login != User.find(self.id).login

end
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-05-08 08:18
(Received via mailing list)
Sorry there should be another line in that method

def validate
  if not self.new_record?
    errors.add( :login, "Can't change your login details") if
                      self.login != User.find(self.id).login
  end
end
1b275ea705cc1671f13105d29684cbec?d=identicon&s=25 Scott Walter (sfwalter)
on 2006-05-08 14:27
(Received via mailing list)
Can you just override the default setter that active records creates in
your model and throw an execption if it gets called?

----------------------------------------------------------------------------------------------------
What's an Intel chip doing in a Mac? A whole lor more that it's ever
done in a PC.

My Digital Life - http://scottwalter.com/blog
Pro:Blog - http://scottwalter.com/problog
Snippets  - http://snippets.scottwalter.com

----- Original Message ----
From: Oleg Frolov <leopardus.vulgaris@gmail.com>
To: rails@lists.rubyonrails.org
Sent: Monday, May 8, 2006 12:53:13 AM
Subject: [Rails] how to make field in model immutable after create

Hello everyone!

I want to have login field in User model which is set only once (on
create), and then it should fail validation if the value is changed.
How can I do this? I dig around for validation and tried

def after_validation_on_update
  unless User.find_by_login(:login)
    errors.add(:login, "is immutable. You can't change it")
  end
end

in model, but somewhat it doesn't work. Any hints on how can I make
field immutable after create?

Peace,
olegf
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-05-08 14:39
(Received via mailing list)
Yep, just do:

class User < ActiveRecord::Base
  def login=(value)
    # Either ignore the call, add an error, or raise an exception
  end
end
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-05-08 15:32
(Received via mailing list)
If you make it so that the setter method throws an exception/stops the
call,
how do you set the login when the record is first created?
E24b2a1d71b7365186a934a09ee6f7c3?d=identicon&s=25 Carl-Johan Kihlbom (Guest)
on 2006-05-08 22:15
(Received via mailing list)
Something along the lines of...

def login= (value)
  if new_record?
    self[:login] = value
  else
    raise "You can't change login"
  end
end
This topic is locked and can not be replied to.