Oleg Frolov
2006-May-08 05:53 UTC
[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
Liquid
2006-May-08 06:05 UTC
[Rails] Re: how to make field in model immutable after create
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
On 5/8/06, Oleg Frolov <leopardus.vulgaris@gmail.com>
wrote:> 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
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
Liquid
2006-May-08 06:17 UTC
[Rails] Re: how to make field in model immutable after create
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
On 5/8/06, Liquid <has.sox@gmail.com> wrote:> 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
>
>
>
>
> On 5/8/06, Oleg Frolov <leopardus.vulgaris@gmail.com> wrote:
> > 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
> > _______________________________________________
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
>
Scott Walter
2006-May-08 12:26 UTC
[Rails] how to make field in model immutable after create
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
_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060508/dddac1ea/attachment.html
Jonathan Viney
2006-May-08 12:37 UTC
[Rails] how to make field in model immutable after create
Yep, just do:
class User < ActiveRecord::Base
def login=(value)
# Either ignore the call, add an error, or raise an exception
end
end
On 5/9/06, Scott Walter <tx_scottwalter@yahoo.com>
wrote:>
> Can you just override the default setter that active records creates in
your
> model and throw an execption if it gets called?
>
----------------------------------------------------------------------------------------------------
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? On 5/8/06, Jonathan Viney <jonathan.viney@gmail.com> wrote:> > Yep, just do: > > class User < ActiveRecord::Base > def login=(value) > # Either ignore the call, add an error, or raise an exception > end > end > > On 5/9/06, Scott Walter <tx_scottwalter@yahoo.com> wrote: > > > > Can you just override the default setter that active records creates in > your > > model and throw an execption if it gets called? > > > ---------------------------------------------------------------------------------------------------- > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060508/c1b69d4a/attachment.html
Carl-Johan Kihlbom
2006-May-08 20:11 UTC
[Rails] how to make field in model immutable after create
Something along the lines of...
def login= (value)
if new_record?
self[:login] = value
else
raise "You can''t change login"
end
end
On 5/8/06, Liquid <has.sox@gmail.com> wrote:> 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?
>
>
>
> On 5/8/06, Jonathan Viney <jonathan.viney@gmail.com> wrote:
> > Yep, just do:
> >
> > class User < ActiveRecord::Base
> > def login=(value)
> > # Either ignore the call, add an error, or raise an exception
> > end
> > end
> >
> > On 5/9/06, Scott Walter < tx_scottwalter@yahoo.com> wrote:
> > >
> > > Can you just override the default setter that active records
creates in
> your
> > > model and throw an execption if it gets called?
> > >
>
----------------------------------------------------------------------------------------------------
> > _______________________________________________
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
>
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>