Erwin
2008-Jan-08 18:51 UTC
half way from re-building an object .... just a little bit of brain..
I found that it''s easy w Rails 2.0 to deserialize ! data = XmlSimple.xml_in(result) v = Hash.from_xml(result).values.first => {"membership_at"=>"2007-03-28T21:25:31Z", "display_name"=>"chane850", "first_name"=>nil, "last_name"=>nil} so now how can I build an ActiveRecord-like object (don''t want to create a table) Membership class with these attributes ? @membership = Membership.new.build( v ) ? doesn'' t work any clue ? 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 -~----------~----~----~----~------~----~------~--~---
Brian Hogan
2008-Jan-08 19:03 UTC
Re: half way from re-building an object .... just a little bit of brain..
Create this class, called tableless.rb in your app/models folder # Table-less model used for any object that needs validation or for # any web form that does not need a database. # Idea and concept from Rick Olson class Tableless < ActiveRecord::Base def self.columns() @columns ||= []; end def self.column(name, sql_type = nil, default = nil, null = true) columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null) end # override the save method to prevent exceptions. def save(validate = true) validate ? valid? : true end end Now just create a new class called Membership (app/models/membership.rb) class Membership < Tableless column :display_name, :string column :first_name, :string column :last_name, :string column :membership_at, :datetime validates_presence_of :display_name, :first_name, :last_name, membership_at end And then get ready to rock. m = Membership.new(hash_from_xml) if m.valid? # do stuff else # do other failure stuff end Throw your other membership business rules in the membership.rb class too. I use this technique for contact forms, etc that don''t need backends. On Jan 8, 2008 12:51 PM, Erwin <yves_dufour-ee4meeAH724@public.gmane.org> wrote:> > I found that it''s easy w Rails 2.0 to deserialize ! > > data = XmlSimple.xml_in(result) > v = Hash.from_xml(result).values.first > > => {"membership_at"=>"2007-03-28T21:25:31Z", > "display_name"=>"chane850", "first_name"=>nil, "last_name"=>nil} > > so now how can I build an ActiveRecord-like object (don''t want to > create a table) > > Membership class with these attributes ? > > @membership = Membership.new.build( v ) ? doesn'' t work > > any clue ? > > 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 -~----------~----~----~----~------~----~------~--~---