Hello, In my model Example, I''ve defined three types as such: class Example < ActiveRecord::Base @@TYPE_A = ''A'' @@TYPE_B = ''B'' @@TYPE_C = ''C'' def Example.select.... end end How can I access these types in my controller. I''m trying the following with my controller named Read: def exm a = Example.TYPE_A end It won''t allow me to declare the types and gives me a syntax error. Any help would be appreciated. Aren''t these just class variable declarations and should be available through the class? -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
Chris Wanstrath
2006-Sep-10 09:06 UTC
Re: accessing model class variables in a controller.
On Sep 9, 2006, at 10:10 PM, Sam Donaldson wrote:> Hello,Hello.> How can I access these types in my controller. I''m trying the > following > with my controller named Read: > > def exm > a = Example.TYPE_A > endThat''s not how class variables work. I haven''t read it, but word on the street is `Ruby for Rails'' by David A. Black is a good resource for learning Ruby with Rails. The Pickaxe is fantastic also (google: Ruby Pickaxe).> It won''t allow me to declare the types and gives me a syntax > error. Any > help would be appreciated. Aren''t these just class variable > declarations and should be available through the class?Yes. What you want is cattr_accessor. It works just like attr_accessor, but for class variables. Class variables can be tricky. Check out this nutty blog post to understand their nuances: http://errtheblog.com/post/22 -- Chris Wanstrath http://errtheblog.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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Sep-10 09:23 UTC
Re: accessing model class variables in a controller.
Hi -- On Sun, 10 Sep 2006, Sam Donaldson wrote:> > Hello, > > In my model Example, I''ve defined three types as such: > > class Example < ActiveRecord::Base > @@TYPE_A = ''A'' > @@TYPE_B = ''B'' > @@TYPE_C = ''C'' > > def Example.select.... > end > end > > How can I access these types in my controller. I''m trying the following > with my controller named Read: > > def exm > a = Example.TYPE_A > end > > It won''t allow me to declare the types and gives me a syntax error. Any > help would be appreciated. Aren''t these just class variable > declarations and should be available through the class?You''re confusing class variables with constants, and constants with methods :-) Class variables (@@var) are variables with a scope that includes 1. the class in whose scope they were created 2. all descendants of that class 3. all instances of any of those classes What class variables *don''t* have is any implication for methods; that is, when you assign to a class variable, you''re just assigning to a variable, not creating any methods that either get or set the value of that variable. If you want such methods, you have to write them: class C def self.cvar_x # access the cvar from the class @@x end def cvar_x # access it from instances of the class @@x end end or create/use a facility for doing so, like the "cattr_*" methods that are used internally in Rails. By the way, the somewhat strange behavior of "class variables" -- in particular, that they''re not actually class-specific, but are more class-hierarchy-specific -- is due to change in future versions of Ruby. Then there are people like me, who think class variables cause (and will continue to cause, even with that change) much more trouble than they''re worth, and wish they would go away entirely. But I don''t think Matz is in that camp :-) David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Sep-10 09:45 UTC
Re: accessing model class variables in a controller.
Hi -- On Sun, 10 Sep 2006, Chris Wanstrath wrote:> > On Sep 9, 2006, at 10:10 PM, Sam Donaldson wrote: > >> Hello, > > Hello. > >> How can I access these types in my controller. I''m trying the >> following >> with my controller named Read: >> >> def exm >> a = Example.TYPE_A >> end > > That''s not how class variables work. I haven''t read it, but word on > the street is `Ruby for Rails'' by David A. Black is a good resource > for learning Ruby with Rails. The Pickaxe is fantastic also (google: > Ruby Pickaxe).I''ve responded in detail to Sam''s question because my book, though I''m happy to see it recommended, says very little about class variables. I like to think it''s not just because I dislike them, but also because I really haven''t seen very many reasons to use them. People learning Rails will definitely want to know them when they see them, but beyond that I didn''t have much to say about them.>> It won''t allow me to declare the types and gives me a syntax >> error. Any >> help would be appreciated. Aren''t these just class variable >> declarations and should be available through the class? > > Yes. What you want is cattr_accessor. It works just like > attr_accessor, but for class variables.It''s misnamed, though, because it implies that class variables can store object "attribute" values. Since they cut across many objects, they''re not really representing any object''s attributes.> Class variables can be tricky. Check out this nutty blog post to > understand their nuances: http://errtheblog.com/post/22I''ve just added a response to that post, which will save some space here :-) David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Chris Wanstrath
2006-Sep-10 09:58 UTC
Re: accessing model class variables in a controller.
On Sep 10, 2006, at 2:45 AM, dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote:> I''ve just added a response to that post, which will save some space > here :-)Great points and great comment. Thanks for the insight! -- Chris Wanstrath http://errtheblog.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 -~----------~----~----~----~------~----~------~--~---
All of this makes this clear. I did want to mention that my use case for these class variables was not to actually write to them, but to just use them to define a set of possible values for a particular attribute in an object. In other words, they are sort of used as a constant, but within the scope of that class. I didn''t want to declare these outside of my model class as constants b/c there''s no reason why they should be available elsewhere. If this design decision does not sound good, I''d like to know why. Thanks. On 9/10/06, Chris Wanstrath <chris-G5sj8e7vJc8@public.gmane.org> wrote:> > > On Sep 10, 2006, at 2:45 AM, dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote: > > > I''ve just added a response to that post, which will save some space > > here :-) > > Great points and great comment. Thanks for the insight! > > -- > Chris Wanstrath > http://errtheblog.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 -~----------~----~----~----~------~----~------~--~---