Chris Gers32
2008-Feb-29 10:50 UTC
find() not returning value of dynamically renamed field
Hi, I have a database table, containing a large number of fields, among which a bunch of i18n fields, like DESC_EN, DESC_FR, DESC_DE, etc. I would like to dynamically select which one of these fields I will return, by using SQL''s "AS" pointing to the same field name: DESCRIPTION_I18N. My Rails code looks something like this: ### MODEL ### def description_i18n # The actual field is selected dynamically: self[:desc_fr] end ### CONTROLLER ### @my_data = MyTable.find( :all, :select => "ID, CODE, DESC_FR AS DESCRIPTION_I18N", :conditions => conditions_list) @my_data_i18n = @my_data.to_xml :methods => [:description_i18n] render :xml => @my_data_i18n ########################## The resulting XML structure lacks a value for the DESCRIPTION_I18N field, unless I comment out the :select option in find()... Unfortunately, I need the :select option to restrict the number of fields in my SELECT statement, otherwise I get way too much data. Can anyone figure out what I''m doing wrong? Thanks, Chris. -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Feb-29 11:20 UTC
Re: find() not returning value of dynamically renamed field
On 29 Feb 2008, at 10:50, Chris Gers32 wrote:> > Hi, > > I have a database table, containing a large number of fields, among > which a bunch of i18n fields, like DESC_EN, DESC_FR, DESC_DE, etc. I > would like to dynamically select which one of these fields I will > return, by using SQL''s "AS" pointing to the same field name: > DESCRIPTION_I18N.does it not like the fact that you have 2 things that are identical apart from the case? Also won''t this fail because the description_i18n method wants the desc_fr field which isn''t available with that name? Fred> > > My Rails code looks something like this: > > ### MODEL ### > > def description_i18n > # The actual field is selected dynamically: > self[:desc_fr] > end > > ### CONTROLLER ### > > @my_data = MyTable.find( > :all, > :select => "ID, CODE, DESC_FR AS DESCRIPTION_I18N", > :conditions => conditions_list) > @my_data_i18n = @my_data.to_xml :methods => [:description_i18n] > render :xml => @my_data_i18n > > ########################## > > The resulting XML structure lacks a value for the DESCRIPTION_I18N > field, unless I comment out the :select option in find()... > > Unfortunately, I need the :select option to restrict the number of > fields in my SELECT statement, otherwise I get way too much data. > > Can anyone figure out what I''m doing wrong? > > Thanks, > > Chris. > -- > 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?hl=en -~----------~----~----~----~------~----~------~--~---
> does it not like the fact that you have 2 things that are identical > apart from the case?That''s not the problem, because the case is irrelevant in SQL (at least as far as Oracle is concerned); this works fine with all other fields.> Also won''t this fail because the description_i18n method wants the > desc_fr field which isn''t available with that name? >I see your point: I don''t need to mention DESCRIPTION_I18N, because Rails creates it in to_xml(). So removing "AS DESCRIPTION_I18N" solved my problem. Thanks again, Chris. -- 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?hl=en -~----------~----~----~----~------~----~------~--~---