Assigning Virtual Attribute in Rails -


i'm attempting understand difference between these 2 ways of assigning virtual attributes in rails 4. 1 of them causes "stack level deep" , 1 of them works fine. i'm attempting use new activerecord array field parsing textfield , splitting commas create tag field. working code follows:

class post < activerecord::base   def tags=(s)     self[:tags] = s.split(/,\s+/)   end end 

however, when change assign tag field in model assigning class variable, causes "stack level deep".

class post < activerecord::base   def tags=(s)     self.tags = s.split(/,\s+/)   end end 

can explain why happens me? seems using self.tags causes virtual attribute run until stack blows up. part of activerecord causes this?

the stack level deep error has nothing rails. basic ruby class this:

class post   def tags=(s)     self.tags = s   end end  > post.new.tags = "a,b,c" systemstackerror: stack level deep      

calling self.tags = re-execute tags=(s) on same object, on , on again. in first example, you're directly setting attribute via self[:tags] =, doesn't re-execute tags=(s) @ all. assume oversimplification of activerecord first example of model's fields.


Comments