Forum: Ruby on Rails how to make "virtual ActiveRecord" be short?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
gpy g. (Guest)
on 2007-06-18 12:08
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
(Guest)
on 2007-06-18 21:36
(Received via mailing list)
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.
gpy g. (Guest)
on 2007-06-19 05:46
removed_email_address@domain.invalid 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
This topic is locked and can not be replied to.