Hi all Oh my god, this really shouldn''t be a tough one, but I just don''t get it. ;-) class Page < ActiveRecord::Base def caption caption ? caption : title end end I want to return the content of the title column when the caption column is empty: p.caption # Caption is not empty # => "caption" p.caption # Caption is empty # => "title" Not astonishing, I get a StackLevelTooDeep Exception with my "solution" above... But how can I do it right? Thanks a lot... My brain is just a bit wrecked today... Josh -- Posted via http://www.ruby-forum.com/.
On Wed, Oct 7, 2009 at 11:11 AM, Joshua Muheim <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> class Page < ActiveRecord::Base > def caption > caption ? caption : title > end > endThis will produce an infinite loop. I think you want something more like self.caption ? self.caption : self.title But honestly I''d rename the method to something else for clarity. -- Greg Donald http://destiney.com/
Philip Hallstrom
2009-Oct-07 16:23 UTC
Re: Return "title" column if "caption" column is empty?
> On Wed, Oct 7, 2009 at 11:11 AM, Joshua Muheim > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> class Page < ActiveRecord::Base >> def caption >> caption ? caption : title >> end >> end > > This will produce an infinite loop. I think you want something more > like > > self.caption ? self.caption : self.title > > But honestly I''d rename the method to something else for clarity.Agreed. At some point you''re going to want to access ''caption'' directly and the above won''t let you do that. Plus your team might not realize you''re adding magic. I''ve always gone with "preferred"... that is.. def preferred_caption # code to get me the ideal caption for the fields available end That helps remind me that I''m not necessarily getting back the caption itself, but the ideal caption for the given situation. It''s worked well for me so far. -philip
Use read_attribute(:caption). 2009/10/7, Joshua Muheim <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Hi all > > Oh my god, this really shouldn''t be a tough one, but I just don''t get > it. ;-) > > class Page < ActiveRecord::Base > def caption > caption ? caption : title > end > end > > I want to return the content of the title column when the caption column > is empty: > > p.caption # Caption is not empty > # => "caption" > > p.caption # Caption is empty > # => "title" > > Not astonishing, I get a StackLevelTooDeep Exception with my "solution" > above... But how can I do it right? > > Thanks a lot... My brain is just a bit wrecked today... > Josh > -- > Posted via http://www.ruby-forum.com/. > > > >-- Von meinen Mobilgerät aus gesendet
Joshua Muheim
2009-Oct-07 20:26 UTC
Re: Return "title" column if "caption" column is empty?
Thanks for your useful hints, guys. :-) -- Posted via http://www.ruby-forum.com/.
Mauricio Szabo
2009-Oct-08 11:56 UTC
Re: Return "title" column if "caption" column is empty?
super might work too def caption super ? super : title end On Oct 7, 5:26 pm, Joshua Muheim <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thanks for your useful hints, guys. :-) > -- > Posted viahttp://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Oct-08 12:06 UTC
Re: Return "title" column if "caption" column is empty?
Mauricio Szabo wrote:> super might work too > > def caption > super ? super : title > endBut it won''t, because caption is not defined on the superclass.> > > On Oct 7, 5:26�pm, Joshua Muheim <rails-mailing-l...-ARtvInVfO7m5VldFQK4jKA@public.gmane.orgt>Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Mauricio Szabo
2009-Oct-08 13:33 UTC
Re: Return "title" column if "caption" column is empty?
Have you tested it? I tried on a test application, and it works... On Oct 8, 9:06 am, Marnen Laibow-Koser <rails-mailing-l...@andreas- s.net> wrote:> Mauricio Szabo wrote: > > super might work too > > > def caption > > super ? super : title > > end > > But it won''t, because caption is not defined on the superclass. > > > > > On Oct 7, 5:26 pm, Joshua Muheim <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Oct-08 13:43 UTC
Re: Return "title" column if "caption" column is empty?
Mauricio Szabo wrote:> Have you tested it? I tried on a test application, and it works...Hmm. Maybe ActiveRecord''s method_missing trickery strikes again...> > On Oct 8, 9:06�am, Marnen Laibow-Koser <rails-mailing-l...@andreas-Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Also I am not sure what rails default is when you just add a "?"....does it only fail if caption is nil? I like to do something like this in these scenarios: def the_caption self.caption.blank? title : caption end I haven''t used read_attribute yet but from the docs it might look something like this def caption caption = read_attribute(:caption) caption.blank? self.title : caption end You get the idea though....just wanted to mention the "blank" part because if your caption is somehow set to " " I am guessing you want the title returned. On Oct 8, 9:43 am, Marnen Laibow-Koser <rails-mailing-l...@andreas- s.net> wrote:> Mauricio Szabo wrote: > > Have you tested it? I tried on a test application, and it works... > > Hmm. Maybe ActiveRecord''s method_missing trickery strikes again... > > > > > On Oct 8, 9:06 am, Marnen Laibow-Koser <rails-mailing-l...@andreas- > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com/.
def caption self[:caption] || title end On Oct 8, 5:11 am, Joshua Muheim <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi all > > Oh my god, this really shouldn''t be a tough one, but I just don''t get > it. ;-) > > class Page < ActiveRecord::Base > def caption > caption ? caption : title > end > end > > I want to return the content of the title column when the caption column > is empty: > > p.caption # Caption is not empty > # => "caption" > > p.caption # Caption is empty > # => "title" > > Not astonishing, I get a StackLevelTooDeep Exception with my "solution" > above... But how can I do it right? > > Thanks a lot... My brain is just a bit wrecked today... > Josh > -- > Posted viahttp://www.ruby-forum.com/.
@Joshua - FYI self[:caption] <=> read_attribute(:caption) @Chris - ...and I am guessing interchangeable with self.caption On Oct 9, 5:27 am, Chris <c.bartl...-wUU9E3n5/m4qAMOr+u8IRA@public.gmane.org> wrote:> def caption > self[:caption] || title > end > > On Oct 8, 5:11 am, Joshua Muheim <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > Hi all > > > Oh my god, this really shouldn''t be a tough one, but I just don''t get > > it. ;-) > > > class Page < ActiveRecord::Base > > def caption > > caption ? caption : title > > end > > end > > > I want to return the content of the title column when the caption column > > is empty: > > > p.caption # Caption is not empty > > # => "caption" > > > p.caption # Caption is empty > > # => "title" > > > Not astonishing, I get a StackLevelTooDeep Exception with my "solution" > > above... But how can I do it right? > > > Thanks a lot... My brain is just a bit wrecked today... > > Josh > > -- > > Posted viahttp://www.ruby-forum.com/.
Afaik it has nothing to do with method missing magic. Its just that if you overwrite a method, a call to super invokes the original method. AR injects accessor methods for every table column into the class. 2009/10/8, Marnen Laibow-Koser <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Mauricio Szabo wrote: >> Have you tested it? I tried on a test application, and it works... > > Hmm. Maybe ActiveRecord''s method_missing trickery strikes again... >> >> On Oct 8, 9:06�am, Marnen Laibow-Koser <rails-mailing-l...@andreas- > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted via http://www.ruby-forum.com/. > > > >-- Von meinen Mobilgerät aus gesendet
Marnen Laibow-Koser
2009-Oct-09 17:36 UTC
Re: Return "title" column if "caption" column is empty?
mike wrote:> Afaik it has nothing to do with method missing magic. Its just that if > you overwrite a method, a call to super invokes the original method.Not really. As far as I know, super calls the method of the same name *in the parent class*, which isn''t always the same thing. That''s why we have alias_method_chain -- it wouldn''t be necessary if super worked the way you claim.> AR injects accessor methods for every table column into the class.But into the model class itself, not AR::Base. There is no AR::Base#caption method for super to call. I suspect that super simply triggers AR''s method_missing, which happens to do the right thing in this case. But that shouldn''t be relied on as a general rule c> > 2009/10/8, Marnen Laibow-Koser <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: >> Marnen Laibow-Koser >> http://www.marnen.org >> marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org >> -- >> Posted via http://www.ruby-forum.com/. >> >> > >> > > -- > Von meinen Mobilgerät aus gesendet-- Posted via http://www.ruby-forum.com/.