Am I correct in thinking that one cannot use attr_accessor in ActiveRecord models? I have success when using either cattr_accessor or mattr_accessor but not the native Ruby method. Am I missing something here, or is this how it really is? Thanks. Elliott G -- 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 _can_ use attr_accessor in ActiveRecord models. Like this: class Article < ActiveRecord::Base attr_accessor :my_prop end Maybe you could post some code and/or error message. /Lasse 2010/3/24 elliottg <x@simplecircle.net>> Am I correct in thinking that one cannot use attr_accessor in > ActiveRecord models? > > I have success when using either cattr_accessor or mattr_accessor but > not the native Ruby method. Am I missing something here, or is this > how it really is? > > Thanks. Elliott G > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@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.
Hi Lasse. I think I may be able to get to the point a bit better with 2 more questions... Am I correct in thinking that attr_accessor always returns or sets an instance variable? Another reason I''m confused, is that, when I set an attr_accessor :foo in my model and then drop into script/console and call .methods on that model instance, I never see "foo" or "foo=" in the methods list when I run a search for them. Conversely when setting either cattr_accessor :foo or mattr_accessor :foo shows the the two new "foo" and "foo=" methods in the model''s method list as expected. What am I missing here? Thanks, Elliott G On Mar 24, 4:28 pm, Lasse Bunk <lasseb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You _can_ use attr_accessor in ActiveRecord models. Like this: > > class Article < ActiveRecord::Base > attr_accessor :my_prop > end > > Maybe you could post some code and/or error message. > > /Lasse > > 2010/3/24 elliottg <x...-+PdThUrr9bOXUBVsB0ZYTw@public.gmane.org> > > > > > Am I correct in thinking that one cannot use attr_accessor in > > ActiveRecord models? > > > I have success when using either cattr_accessor or mattr_accessor but > > not the native Ruby method. Am I missing something here, or is this > > how it really is? > > > Thanks. Elliott G > > > -- > > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > . > > 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.
Thats because mattr_accessor is at module level, cattr_accessor is at class level, and attr_accessor is at instance level. So you have to use class.instance_methods to find your method – like this: class_name.instance_methods.grep(/method_name/) /Lasse 2010/3/24 elliottg <x@simplecircle.net>> Hi Lasse. > > I think I may be able to get to the point a bit better with 2 more > questions... > > Am I correct in thinking that attr_accessor always returns or sets an > instance variable? > > Another reason I''m confused, is that, when I set an attr_accessor :foo > in my model and then drop into script/console and call .methods on > that model instance, I never see "foo" or "foo=" in the methods list > when I run a search for them. Conversely when setting either > cattr_accessor :foo or mattr_accessor :foo shows the the two new > "foo" and "foo=" methods in the model''s method list as expected. > > What am I missing here? > > Thanks, Elliott G > > On Mar 24, 4:28 pm, Lasse Bunk <lasseb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > You _can_ use attr_accessor in ActiveRecord models. Like this: > > > > class Article < ActiveRecord::Base > > attr_accessor :my_prop > > end > > > > Maybe you could post some code and/or error message. > > > > /Lasse > > > > 2010/3/24 elliottg <x...-+PdThUrr9bOXUBVsB0ZYTw@public.gmane.org> > > > > > > > > > Am I correct in thinking that one cannot use attr_accessor in > > > ActiveRecord models? > > > > > I have success when using either cattr_accessor or mattr_accessor but > > > not the native Ruby method. Am I missing something here, or is this > > > how it really is? > > > > > Thanks. Elliott G > > > > > -- > > > 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm > . > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org><rubyonrails-talk%2Bunsubscrib > e@googlegroups.com> > > > . > > > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@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.
That clearly explains some of the issues I was having. Thanks a ton! EG On Mar 24, 5:36 pm, Lasse Bunk <lasseb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thats because mattr_accessor is at module level, cattr_accessor is at class > level, and attr_accessor is at instance level. So you have to use > class.instance_methods to find your method – like this: > > class_name.instance_methods.grep(/method_name/) > > /Lasse > > 2010/3/24 elliottg <x...-+PdThUrr9bOXUBVsB0ZYTw@public.gmane.org> > > > > > Hi Lasse. > > > I think I may be able to get to the point a bit better with 2 more > > questions... > > > Am I correct in thinking that attr_accessor always returns or sets an > > instance variable? > > > Another reason I''m confused, is that, when I set an attr_accessor :foo > > in my model and then drop into script/console and call .methods on > > that model instance, I never see "foo" or "foo=" in the methods list > > when I run a search for them. Conversely when setting either > > cattr_accessor :foo or mattr_accessor :foo shows the the two new > > "foo" and "foo=" methods in the model''s method list as expected. > > > What am I missing here? > > > Thanks, Elliott G > > > On Mar 24, 4:28 pm, Lasse Bunk <lasseb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > You _can_ use attr_accessor in ActiveRecord models. Like this: > > > > class Article < ActiveRecord::Base > > > attr_accessor :my_prop > > > end > > > > Maybe you could post some code and/or error message. > > > > /Lasse > > > > 2010/3/24 elliottg <x...-+PdThUrr9bOXUBVsB0ZYTw@public.gmane.org> > > > > > Am I correct in thinking that one cannot use attr_accessor in > > > > ActiveRecord models? > > > > > I have success when using either cattr_accessor or mattr_accessor but > > > > not the native Ruby method. Am I missing something here, or is this > > > > how it really is? > > > > > Thanks. Elliott G > > > > > -- > > > > 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@googlegroups.com > > . > > > > To unsubscribe from this group, send email to > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > e@googlegroups.com> > > > > . > > > > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > . > > 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.