Non-Persistant Models – BaseWithoutTable

18 06 2007

I recently wanted to create a simple email the took a name, email address and a message, and then send me an email. I then though to myself, this doesn’t look to be possible using the standard Rails MVC conventions. After a little googling I turned up this and a basic tutorial on how to use it here.

These are basically the steps :

1. Install the plugin

script/install http://svn.viney.net.nz/things/rails/plugins/active_record_base_without_table/

2. Create your controllers models and views as normal.

3. In your model you need to extend ActiveRecord with BaseWithoutTable rather than just Base.

class ModelName < ActiveRecord::BaseWithoutTable  column :name, :string
  column :email_address, :string
  column :subject, :string
  column :message, :text

  validates_presence_of :name, :email_address, :subject, :string
  validates_format_of :email_address, :with => /^([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i
  validates_length_of :email_address, :within => 5..255
end

4. …and the rest is as normal…

Enjoy, it certainly save me a lot of bother.

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: