I''m a little confused when I should be using ''self'' in my model. I had code like: class User < ActiveRecord::Base before_save :do_something def do_something self.user_bio_text = .... .. self.user_bio_text end end If I removed ''self'', it didn''t seem to set the model''s attribute at all (it would return nil). I can''t recall exaclty where this happenend in my code, but I remember that I was trying to get the a model''s attribute and it didn''t work when I used ''self.some_attribute''. So I''m confused, when do I use ''self'' and when''t don''t I? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On May 4, 8:34 pm, S Ahmed <sahmed1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m a little confused when I should be using ''self'' in my model. > > I had code like: > > class User < ActiveRecord::Base > > before_save :do_something > > def do_something > > self.user_bio_text = .... > .. > self.user_bio_text > end > > end > > If I removed ''self'', it didn''t seem to set the model''s attribute at all (it > would return nil). >Ruby doesn''t know whether foo = blah means set the local variable foo or call your foo= accessor method and default to creating the local variable, so if you did want to call the accessor method you need to disambiguate it, with self.foo Fred> I can''t recall exaclty where this happenend in my code, but I remember that > I was trying to get the a model''s attribute and it didn''t work when I used > ''self.some_attribute''. > > So I''m confused, when do I use ''self'' and when''t don''t I?-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, May 5, 2011 at 3:34 AM, S Ahmed <sahmed1020-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m a little confused when I should be using ''self'' in my model. > > I had code like: > > class User < ActiveRecord::Base > > before_save :do_something > > > def do_something > > self.user_bio_text = .... > .. > self.user_bio_text > end > > > end > > > > If I removed ''self'', it didn''t seem to set the model''s attribute at all (it > would return nil). > >when assigning values to attributes of an instance object, you need to use self, ie self.attribute = something. if you''re only getting the value of that attribute, no need to add self.> I can''t recall exaclty where this happenend in my code, but I remember that > I was trying to get the a model''s attribute and it didn''t work when I used > ''self.some_attribute''. > > > So I''m confused, when do I use ''self'' and when''t don''t I? > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
ah I see, this is what I have experienced. But why is that? Shouldn''t both getting and setting have similiar behaviour? I think when I was getting a value, using self.attribute wasn''t working, is that the case or was I doing somehting else wrong? On Wed, May 4, 2011 at 8:31 PM, Jim Ruther Nill <jvnill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Thu, May 5, 2011 at 3:34 AM, S Ahmed <sahmed1020-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> I''m a little confused when I should be using ''self'' in my model. >> >> I had code like: >> >> class User < ActiveRecord::Base >> >> before_save :do_something >> >> >> def do_something >> >> self.user_bio_text = .... >> .. >> self.user_bio_text >> end >> >> >> end >> >> >> >> If I removed ''self'', it didn''t seem to set the model''s attribute at all >> (it would return nil). >> >> > when assigning values to attributes of an instance object, you need to use > self, ie self.attribute = something. > if you''re only getting the value of that attribute, no need to add self. > > >> I can''t recall exaclty where this happenend in my code, but I remember >> that I was trying to get the a model''s attribute and it didn''t work when I >> used ''self.some_attribute''. >> >> >> So I''m confused, when do I use ''self'' and when''t don''t I? >> >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > > > -- > ------------------------------------------------------------- > visit my blog at http://jimlabs.heroku.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, May 5, 2011 at 9:41 AM, S Ahmed <sahmed1020-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ah I see, this is what I have experienced. > > But why is that? Shouldn''t both getting and setting have similiar > behaviour? > > I think when I was getting a value, using self.attribute wasn''t working, is > that the case or was I doing somehting else wrong? > > >when you want to get a value, you can use self or not but it should return the same value unless, as Fred pointed out, you set a local variable with the same name as one of the instance object''s attribute where using self.attribute would return the attribute value, and using attribute (which is also the local variable name) would return the variable value.> > > On Wed, May 4, 2011 at 8:31 PM, Jim Ruther Nill <jvnill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> >> >> On Thu, May 5, 2011 at 3:34 AM, S Ahmed <sahmed1020-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>> I''m a little confused when I should be using ''self'' in my model. >>> >>> I had code like: >>> >>> class User < ActiveRecord::Base >>> >>> before_save :do_something >>> >>> >>> def do_something >>> >>> self.user_bio_text = .... >>> .. >>> self.user_bio_text >>> end >>> >>> >>> end >>> >>> >>> >>> If I removed ''self'', it didn''t seem to set the model''s attribute at all >>> (it would return nil). >>> >>> >> when assigning values to attributes of an instance object, you need to use >> self, ie self.attribute = something. >> if you''re only getting the value of that attribute, no need to add self. >> >> >>> I can''t recall exaclty where this happenend in my code, but I remember >>> that I was trying to get the a model''s attribute and it didn''t work when I >>> used ''self.some_attribute''. >>> >>> >>> So I''m confused, when do I use ''self'' and when''t don''t I? >>> >>> -- >>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> To unsubscribe from this group, send email to >>> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> For more options, visit this group at >>> http://groups.google.com/group/rubyonrails-talk?hl=en. >>> >> >> >> >> -- >> ------------------------------------------------------------- >> visit my blog at http://jimlabs.heroku.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On May 5, 2:41 am, S Ahmed <sahmed1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ah I see, this is what I have experienced. > > But why is that? Shouldn''t both getting and setting have similiar > behaviour? > > I think when I was getting a value, using self.attribute wasn''t working, is > that the case or was I doing somehting else wrong? >There is the same ambiguity when getting a value, however (unlike when setting) if there is no local variable called foo then ruby can assume that foo means self.foo. If there was such a local variable then you''d need to disambiguate in the same way Fred> On Wed, May 4, 2011 at 8:31 PM, Jim Ruther Nill <jvn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > On Thu, May 5, 2011 at 3:34 AM, S Ahmed <sahmed1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> I''m a little confused when I should be using ''self'' in my model. > > >> I had code like: > > >> class User < ActiveRecord::Base > > >> before_save :do_something > > >> def do_something > > >> self.user_bio_text = .... > >> .. > >> self.user_bio_text > >> end > > >> end > > >> If I removed ''self'', it didn''t seem to set the model''s attribute at all > >> (it would return nil). > > > when assigning values to attributes of an instance object, you need to use > > self, ie self.attribute = something. > > if you''re only getting the value of that attribute, no need to add self. > > >> I can''t recall exaclty where this happenend in my code, but I remember > >> that I was trying to get the a model''s attribute and it didn''t work when I > >> used ''self.some_attribute''. > > >> So I''m confused, when do I use ''self'' and when''t don''t I? > > >> -- > >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >> For more options, visit this group at > >>http://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- > > ------------------------------------------------------------- > > visit my blog athttp://jimlabs.heroku.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
got it. I just want to understand, why is it different when setting a value, why doesn''t it assume I want self.attribute1 if there was no local variable with the same name e.g. ''attribute1''. On Thu, May 5, 2011 at 2:00 AM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On May 5, 2:41 am, S Ahmed <sahmed1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > ah I see, this is what I have experienced. > > > > But why is that? Shouldn''t both getting and setting have similiar > > behaviour? > > > > I think when I was getting a value, using self.attribute wasn''t working, > is > > that the case or was I doing somehting else wrong? > > > There is the same ambiguity when getting a value, however (unlike when > setting) > if there is no local variable called foo then ruby can assume that foo > means self.foo. > If there was such a local variable then you''d need to disambiguate in > the same way > > Fred > > > On Wed, May 4, 2011 at 8:31 PM, Jim Ruther Nill <jvn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > > > > > > > > > On Thu, May 5, 2011 at 3:34 AM, S Ahmed <sahmed1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > >> I''m a little confused when I should be using ''self'' in my model. > > > > >> I had code like: > > > > >> class User < ActiveRecord::Base > > > > >> before_save :do_something > > > > >> def do_something > > > > >> self.user_bio_text = .... > > >> .. > > >> self.user_bio_text > > >> end > > > > >> end > > > > >> If I removed ''self'', it didn''t seem to set the model''s attribute at > all > > >> (it would return nil). > > > > > when assigning values to attributes of an instance object, you need to > use > > > self, ie self.attribute = something. > > > if you''re only getting the value of that attribute, no need to add > self. > > > > >> I can''t recall exaclty where this happenend in my code, but I remember > > >> that I was trying to get the a model''s attribute and it didn''t work > when I > > >> used ''self.some_attribute''. > > > > >> So I''m confused, when do I use ''self'' and when''t don''t I? > > > > >> -- > > >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > >> To unsubscribe from this group, send email to > > >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > >> For more options, visit this group at > > >>http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > -- > > > ------------------------------------------------------------- > > > visit my blog athttp://jimlabs.heroku.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 5 May 2011 15:23, S Ahmed <sahmed1020-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I just want to understand, why is it different when setting a value, why > doesn''t it assume I want self.attribute1 if there was no local variable with > the same name e.g. ''attribute1''.How would you ever create that attribute1 local variable if the setting assumed it should use self.attribute1 ?! :-) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.