use ActiveRecord::ConnectionAdapters::Column can make a "virtual ActiveRecord",but how to make it short or have it other method? class EmailForm < 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 column :title, :string column :body, :string validates_presence_of :title validates_presence_of :body end -- 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 -~----------~----~----~----~------~----~------~--~---
mboeh-UtljGsy+VVc0aPf6s/0I7w@public.gmane.org
2007-Jun-18 17:35 UTC
Re: how to make "virtual ActiveRecord" be short?
I''ve got a mixin that approaches this from the other angle: http://djur.desperance.net/ruby/fake_active_record.rb It implements enough of the AR interface to make most validations work; human_attribute_name is implemented so error_messages_for works. All you have to do is ''include FakeActiveRecord'' and define the attributes with attr_accessor. On Jun 18, 1:08 am, gz zz <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> use ActiveRecord::ConnectionAdapters::Column can make a "virtual > ActiveRecord",but how to make it short or have it other method? > > class EmailForm < 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 > > column :title, :string > column :body, :string > > validates_presence_of :title > validates_presence_of :body > > end > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
mboeh-UtljGsy+VVc0aPf6s/0I7w@public.gmane.org wrote:> I''ve got a mixin that approaches this from the other angle: > > http://djur.desperance.net/ruby/fake_active_record.rb > > It implements enough of the AR interface to make most validations > work; human_attribute_name is implemented so error_messages_for works. > All you have to do is ''include FakeActiveRecord'' and define the > attributes with attr_accessor.thank you.the error_message does not show:("can not be blank") model: require "fake_active_record" class Fake < ActiveRecord::Base include FakeActiveRecord attr_accessor :title validates_presence_of :title,:message=>"can not be blank" end view: <%=error_messages_for :fake%> <%form_tag :action=>"fake" do %> <%fields_for :fake do |f|%> <%=f.text_field :title%><%=error_message_on(:fake,:title)%> <%end%> <%=submit_tag%> <%end%> controller: def fake #if Fake.new(params[:fake]) #else render :action=>"fake_new" #end end def fake_new @fake = Fake.new end -- 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 -~----------~----~----~----~------~----~------~--~---