Hi! I''m new to Rails! Rails rox! 1 quesion so far: I have :email attribute in User model. I dont'' want :email to allow to be updated. How do i do this with Rails? Do I have to implement required validation manually? Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 1/26/07, gmarik <gmarik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi! > I''m new to Rails! > Rails rox! > > 1 quesion so far: > I have :email attribute in User model. > I dont'' want :email to allow to be updated. > > How do i do this with Rails? > Do I have to implement required validation manually? > > Thanks! > > > > >classs User < ActiveRecord::Base attr_protected :email ... end Docs: http://caboo.se/doc/classes/ActiveRecord/Base.html#M006884 -- Zack Chandler http://depixelate.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Zak, but it''s not what i need. I don''t want it to be UPDATED(ie Edit) And i need this attribute, when object is being CREATED(ie New). So it could look like this(the code isnt'' valid): .... attr_accessible :name, :description attr_accessible :email if new_record? .... Any ideas? Thanks! On Jan 27, 4:55 am, "Zack Chandler" <zackchand...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 1/26/07, gmarik <gma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Hi! > > I''m new to Rails! > > Rails rox! > > > 1 quesion so far: > > I have :email attribute in User model. > > I dont'' want :email to allow to be updated. > > > How do i do this with Rails? > > Do I have to implement required validation manually? > > > Thanks!classs User < ActiveRecord::Base > attr_protected :email > ... > end > > Docs:http://caboo.se/doc/classes/ActiveRecord/Base.html#M006884 > > -- > Zack Chandlerhttp://depixelate.com--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''d say you should do something like... attr_accessor :address attr_protected :email def before_create self.email = address end and use User#address in the form. RSL On 1/29/07, gmarik <gmarik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Thanks Zak, but it''s not what i need. > I don''t want it to be UPDATED(ie Edit) > And i need this attribute, when object is being CREATED(ie New). > > So it could look like this(the code isnt'' valid): > .... > attr_accessible :name, :description > attr_accessible :email if new_record? > .... > > Any ideas? > Thanks! > > On Jan 27, 4:55 am, "Zack Chandler" <zackchand...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 1/26/07, gmarik <gma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > > > > > Hi! > > > I''m new to Rails! > > > Rails rox! > > > > > 1 quesion so far: > > > I have :email attribute in User model. > > > I dont'' want :email to allow to be updated. > > > > > How do i do this with Rails? > > > Do I have to implement required validation manually? > > > > > Thanks!classs User < ActiveRecord::Base > > attr_protected :email > > ... > > end > > > > Docs:http://caboo.se/doc/classes/ActiveRecord/Base.html#M006884 > > > > -- > > Zack Chandlerhttp://depixelate.com > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nice trick! Thanks Russell! On Jan 29, 3:46 pm, "Russell Norris" <sco...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''d say you should do something like... > > attr_accessor :address > attr_protected :email > > def before_create > self.email = address > end > > and use User#address in the form. > > RSL > > On 1/29/07, gmarik <gma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Thanks Zak, but it''s not what i need. > > I don''t want it to be UPDATED(ie Edit) > > And i need this attribute, when object is being CREATED(ie New). > > > So it could look like this(the code isnt'' valid): > > .... > > attr_accessible :name, :description > > attr_accessible :email if new_record? > > .... > > > Any ideas? > > Thanks! > > > On Jan 27, 4:55 am, "Zack Chandler" <zackchand...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 1/26/07, gmarik <gma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi! > > > > I''m new to Rails! > > > > Rails rox! > > > > > 1 quesion so far: > > > > I have :email attribute in User model. > > > > I dont'' want :email to allow to be updated. > > > > > How do i do this with Rails? > > > > Do I have to implement required validation manually? > > > > > Thanks!classs User < ActiveRecord::Base > > > attr_protected :email > > > ... > > > end > > > > Docs:http://caboo.se/doc/classes/ActiveRecord/Base.html#M006884 > > > > -- > > > Zack Chandlerhttp://depixelate.com--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 1/29/07, gmarik <gmarik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Thanks Zak, but it''s not what i need. > I don''t want it to be UPDATED(ie Edit) > And i need this attribute, when object is being CREATED(ie New). > > So it could look like this(the code isnt'' valid): > .... > attr_accessible :name, :description > attr_accessible :email if new_record? > ....attr_readonly -- see <http://dev.rubyonrails.org/ticket/6896> -- may be what you''re looking for? I''m not overly optimistic about our chances of getting it added, though.. Another option for emulating read only attributes could be a before_save callback that; locks the row (select for update), then load the model into some variable and then copy the field in question into self. I''m not sure Russel''s approach will work; say you load the model, the row/column is changed by some other process, and then you save your old model again overwriting the new value. I''d just go with attr_protected and set that field individually on creation, if this is all you need. Isak> Any ideas? > Thanks! > > On Jan 27, 4:55 am, "Zack Chandler" <zackchand...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 1/26/07, gmarik <gma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > > > > > Hi! > > > I''m new to Rails! > > > Rails rox! > > > > > 1 quesion so far: > > > I have :email attribute in User model. > > > I dont'' want :email to allow to be updated. > > > > > How do i do this with Rails? > > > Do I have to implement required validation manually? > > > > > Thanks!classs User < ActiveRecord::Base > > attr_protected :email > > ... > > end > > > > Docs:http://caboo.se/doc/classes/ActiveRecord/Base.html#M006884 > > > > -- > > Zack Chandlerhttp://depixelate.com > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Well, for now Russel''s approach is just fine. The issue with other processes modifying attribute directly can be avoided by putting attr_reader for :email, so it'' won''t be changed from outside. Right? Thanks! On Jan 30, 7:37 pm, "Isak Hansen" <isak.han...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 1/29/07, gmarik <gma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Thanks Zak, but it''s not what i need. > > I don''t want it to be UPDATED(ie Edit) > > And i need this attribute, when object is being CREATED(ie New). > > > So it could look like this(the code isnt'' valid): > > .... > > attr_accessible :name, :description > > attr_accessible :email if new_record? > > .... > > attr_readonly -- see <http://dev.rubyonrails.org/ticket/6896> -- may > be what you''re looking for? I''m not overly optimistic about our > chances of getting it added, though.. > > Another option for emulating read only attributes could be a > before_save callback that; locks the row (select for update), then > load the model into some variable and then copy the field in question > into self. > > I''m not sure Russel''s approach will work; say you load the model, the > row/column is changed by some other process, and then you save your > old model again overwriting the new value. > > I''d just go with attr_protected and set that field individually on > creation, if this is all you need. > > Isak > > > Any ideas? > > Thanks! > > > On Jan 27, 4:55 am, "Zack Chandler" <zackchand...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 1/26/07, gmarik <gma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi! > > > > I''m new to Rails! > > > > Rails rox! > > > > > 1 quesion so far: > > > > I have :email attribute in User model. > > > > I dont'' want :email to allow to be updated. > > > > > How do i do this with Rails? > > > > Do I have to implement required validation manually? > > > > > Thanks!classs User < ActiveRecord::Base > > > attr_protected :email > > > ... > > > end > > > > Docs:http://caboo.se/doc/classes/ActiveRecord/Base.html#M006884 > > > > -- > > > Zack Chandlerhttp://depixelate.com--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 1/31/07, gmarik <gmarik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Well, for now Russel''s approach is just fine. > > The issue with other processes modifying attribute directly can be > avoided by putting attr_reader for :email, so it'' won''t be changed > from outside. Right? >If your app is the only thing writing to that particular field, protecting the attribute in your model - either with attr_protected, an overridden mutator, or the dummy attribute idea - is probably enough, yea. Isak> Thanks! > > > On Jan 30, 7:37 pm, "Isak Hansen" <isak.han...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 1/29/07, gmarik <gma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > Thanks Zak, but it''s not what i need. > > > I don''t want it to be UPDATED(ie Edit) > > > And i need this attribute, when object is being CREATED(ie New). > > > > > So it could look like this(the code isnt'' valid): > > > .... > > > attr_accessible :name, :description > > > attr_accessible :email if new_record? > > > .... > > > > attr_readonly -- see <http://dev.rubyonrails.org/ticket/6896> -- may > > be what you''re looking for? I''m not overly optimistic about our > > chances of getting it added, though.. > > > > Another option for emulating read only attributes could be a > > before_save callback that; locks the row (select for update), then > > load the model into some variable and then copy the field in question > > into self. > > > > I''m not sure Russel''s approach will work; say you load the model, the > > row/column is changed by some other process, and then you save your > > old model again overwriting the new value. > > > > I''d just go with attr_protected and set that field individually on > > creation, if this is all you need. > > > > Isak > > > > > Any ideas? > > > Thanks! > > > > > On Jan 27, 4:55 am, "Zack Chandler" <zackchand...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On 1/26/07, gmarik <gma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Hi! > > > > > I''m new to Rails! > > > > > Rails rox! > > > > > > > 1 quesion so far: > > > > > I have :email attribute in User model. > > > > > I dont'' want :email to allow to be updated. > > > > > > > How do i do this with Rails? > > > > > Do I have to implement required validation manually? > > > > > > > Thanks!classs User < ActiveRecord::Base > > > > attr_protected :email > > > > ... > > > > end > > > > > > Docs:http://caboo.se/doc/classes/ActiveRecord/Base.html#M006884 > > > > > > -- > > > > Zack Chandlerhttp://depixelate.com > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Seemingly Similar Threads
- image file URL generated by image_tag include some parameter
- Using a nonstandard foreign key
- assert_select VS assert_no_tag
- attr_accessible on some properties + attr_protected on others makes class 'open-by-default'
- Testing attr_accessible (and/or attr_protected)