Matt Yoder
2013-Nov-20 17:34 UTC
add class variable to all models using concern to extend AR?
Hi All, I''ve previously used concerns to extend my models without problems. I''m now trying to extend all my models in one swell-foop by extending AR with a concern. In brief, I want to add a class variable to each individual model using a concern that extends AR. Specifically - I need to find the where (and how) to define a class variable scoped to a specific model in my AR extending concern. Is this sane? Possible? Thanks, Matt -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/7142832fd8a1f54f445b9bcaa0e22ae4%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Tamara Temple
2013-Nov-21 09:21 UTC
Re: add class variable to all models using concern to extend AR?
On Nov 20, 2013, at 9:34 AM, Matt Yoder <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi All, > > I''ve previously used concerns to extend my models without problems. I''m > now trying to extend all my models in one swell-foop by extending AR > with a concern. In brief, I want to add a class variable to each > individual model using a concern that extends AR. Specifically - I > need to find the where (and how) to define a class variable scoped to a > specific model in my AR extending concern. Is this sane? Possible? > > Thanks, > > Matt > > —Maybe? This seems like requiring the children to know a specific implementation detail of a parent class via the concern mixin. Instead, I’d provide accessor methods. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/54E853DE-5F8D-492F-9D9A-9041393128D4%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Matt Jones
2013-Nov-21 16:09 UTC
Re: add class variable to all models using concern to extend AR?
On Wednesday, 20 November 2013 12:34:37 UTC-5, Ruby-Forum.com User wrote:> > Hi All, > > I''ve previously used concerns to extend my models without problems. I''m > now trying to extend all my models in one swell-foop by extending AR > with a concern. In brief, I want to add a class variable to each > individual model using a concern that extends AR. Specifically - I > need to find the where (and how) to define a class variable scoped to a > specific model in my AR extending concern. Is this sane? Possible? > >You don''t want a class variable for this - they are shared between all the subclasses. A sample IRB trace: irb: class Foo>> end===> nil irb: module Bar>> def wat=(value) >> @@wat = value >> end >> >> def wat >> @@wat >> end >> end===> nil irb: Foo.send(:include, Bar) ===> Foo irb: f = Foo.new ===> #<Foo:0x00000100b5a008> irb: f.wat = ''huh'' ===> "huh" irb: f.wat ===> "huh" irb: class Baz < Foo>> end===> nil irb: class Baz2 < Foo>> end===> nil irb: b = Baz.new ===> #<Baz:0x00000100b7ac68> irb: b.wat ===> "huh" irb: b2 = Baz2.new ===> #<Baz2:0x00000100b82580> irb: b2.wat = ''nope'' ===> "nope" irb: f.wat ===> "nope" You likely want a class instance variable instead, to avoid the sharing. More details here: http://www.railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/ --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e451922c-9f53-4aeb-802f-e5dadcb2676b%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Matt Yoder
2013-Nov-21 16:22 UTC
Re: add class variable to all models using concern to extend AR?
Thanks Matt, Your observations are exactly what I was running into. Good to know I should focus on class instance variables. Inheritance is a bit of mixed bag- I want it for all subclasses of my models whose superclass is not AR::Base. The link is very helpful, wish I would have seen it earlier. A couple followups- can you comment on whether some of the approach therein has been replaced with the use of ''class_attribute''? Also, do you forsee any issues with using a Concern rather than the extend approach described there? At present I''ve fallen back to just extending individual models (this in retrospect might be safer in the long run, albeit not as magikal). Thanks again for your help, Matt -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b9cd7c2110a1acb6fd2b3739834f14d2%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.