michael_teter
2009-Jan-09 16:42 UTC
Confused about to_xml() in ActiveRecord::Base subclass
Since all my efforts to control how to_xml() is formatting dates has failed, I''m now considering writing my own to_xml(). However, from the limited examples I''ve found, I just don''t understand how to actually reference the columns for the records in my record set. Here''s a simplified view of my ActiveRecord object: class Errors < ActiveRecord::Base set_table_name "legacy_errors" # legacy_errors has three columns: id, msg, error_date # Since the default to_xml is generating error_dates in the format "Mon Nov 03 00:00:00 -0600 2008", # and I cannot seem to force a different behavior, I want to create my own to_xml. # Examples online show the following as how to override to_xml: def to_xml(options = {}) options[:indent] ||= 2 xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) xml.instruct! unless options[:skip_instruct] # v v v xml.level_one do xml.tag!(:second_level, ''content'') end # ^ ^ ^ end end The code between the v and ^ markers is where the body of each XML object is generated. If I were generating by hand, I would do something like this: errors.each do |err| s << "<id>" << err.id << "</id>" s << "<msg>" << err.msg << "</msg>" s << "<error_date>" << err.error_date.strftime("%Y-%m-%d") << "</ error_date>" end I''m not at all understanding how to do my thing within the v ^ section of to_xml(). Any help is appreciated! Thanks. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2009-Jan-09 17:20 UTC
Re: Confused about to_xml() in ActiveRecord::Base subclass
On Jan 9, 2009, at 11:42 AM, michael_teter wrote:> Since all my efforts to control how to_xml() is formatting dates has > failed, I''m now considering writing my own to_xml(). However, from > the limited examples I''ve found, I just don''t understand how to > actually reference the columns for the records in my record set. > > Here''s a simplified view of my ActiveRecord object: > > class Errors < ActiveRecord::Base > set_table_name "legacy_errors" > > # legacy_errors has three columns: id, msg, error_date > # Since the default to_xml is generating error_dates in the format > "Mon Nov 03 00:00:00 -0600 2008", > # and I cannot seem to force a different behavior, I want to > create my own to_xml. > > # Examples online show the following as how to override to_xml: > def to_xml(options = {}) > options[:indent] ||= 2 > xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => > options[:indent]) > xml.instruct! unless options[:skip_instruct] > # v v v > xml.level_one do > xml.tag!(:second_level, ''content'') > end > # ^ ^ ^ > end > end > > The code between the v and ^ markers is where the body of each XML > object is generated. If I were generating by hand, I would do > something like this: > errors.each do |err| > s << "<id>" << err.id << "</id>" > s << "<msg>" << err.msg << "</msg>" > s << "<error_date>" << err.error_date.strftime("%Y-%m-%d") << "</ > error_date>" > end > > I''m not at all understanding how to do my thing within the v ^ section > of to_xml(). > > Any help is appreciated! > > Thanks.that should be the encoding of a single Errors instance. xml.error do |x| x.id(self.id) x.msg(self.msg) x.error_date(self.error_date.strftime(''%Y-%m-%d'')) end I think that you''d get a bunch of errors with: Errors.find(:all, :conditions => [...]).to_xml(:root => ''errors'') By default, I get output of attributes like this: <created-at type="datetime">2009-01-09T02:58:31Z</created-at> <id type="integer">110</id> <reference>From Anne</reference> When I do #to_xml. Notice the date format for the type="datetime". What is the actual class of error_date? Is it a Date or a DateTime or a Time? -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.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?hl=en -~----------~----~----~----~------~----~------~--~---
michael_teter
2009-Jan-09 18:01 UTC
Re: Confused about to_xml() in ActiveRecord::Base subclass
Rob, Thanks much for the good response. To answer your question about the data type, it''s "date" in Oracle. When I use the default to_xml() (without suppressing types and the dasherizer) it looks like this: <error-date type=\"ActiveRecord::ConnectionAdapters::OracleColumn \">Wed Nov 12 00:00:00 -0600 2008</error-date> In fact, every element is listed as "OracleColumn". I did try the following to control my date presentation: proc = Proc.new {|options| options[:builder].tag! (''error_date_x'', options[:object].error_date.strftime("%Y-%m-%d"))} s = errs.to_xml(:dasherize => false, :skip_types => true, :procs => [proc]) That just blows up. NoMethodError: You have a nil object when you didn''t expect it! The error occurred while evaluating nil.error_date What''s really odd is I''ve been using the default to_xml() for months and receiving "YYYY-MM-DD" as my format. Now suddenly I''m getting a different format. Perhaps I updated gems and got something new... So now, if I cannot simply control the formatting of that one column, I''ll do my own to_xml() (following your example). On Jan 9, 11:20 am, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Jan 9, 2009, at 11:42 AM, michael_teter wrote: > > > > > Since all my efforts to control how to_xml() is formatting dates has > > failed, I''m now considering writing my own to_xml(). However, from > > the limited examples I''ve found, I just don''t understand how to > > actually reference the columns for the records in my record set. > > > Here''s a simplified view of my ActiveRecord object: > > > class Errors < ActiveRecord::Base > > set_table_name "legacy_errors" > > > # legacy_errors has three columns: id, msg, error_date > > # Since the default to_xml is generating error_dates in the format > > "Mon Nov 03 00:00:00 -0600 2008", > > # and I cannot seem to force a different behavior, I want to > > create my own to_xml. > > > # Examples online show the following as how to override to_xml: > > def to_xml(options = {}) > > options[:indent] ||= 2 > > xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => > > options[:indent]) > > xml.instruct! unless options[:skip_instruct] > > # v v v > > xml.level_one do > > xml.tag!(:second_level, ''content'') > > end > > # ^ ^ ^ > > end > > end > > > The code between the v and ^ markers is where the body of each XML > > object is generated. If I were generating by hand, I would do > > something like this: > > errors.each do |err| > > s << "<id>" << err.id << "</id>" > > s << "<msg>" << err.msg << "</msg>" > > s << "<error_date>" << err.error_date.strftime("%Y-%m-%d") << "</ > > error_date>" > > end > > > I''m not at all understanding how to do my thing within the v ^ section > > of to_xml(). > > > Any help is appreciated! > > > Thanks. > > that should be the encoding of a single Errors instance. > > xml.error do |x| > x.id(self.id) > x.msg(self.msg) > x.error_date(self.error_date.strftime(''%Y-%m-%d'')) > end > > I think that you''d get a bunch of errors with: > > Errors.find(:all, :conditions => [...]).to_xml(:root => ''errors'') > > By default, I get output of attributes like this: > <created-at type="datetime">2009-01-09T02:58:31Z</created-at> > <id type="integer">110</id> > <reference>From Anne</reference> > When I do #to_xml. Notice the date format for the type="datetime". > What is the actual class of error_date? Is it a Date or a DateTime or > a Time? > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---