Agile Web Development

Build it. Launch it. Love it.

ActiveRecord::Base Without Table

Get the power of ActiveRecord models, including validation, without having a table in the database.

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

    validates_presence_of :name, :email_address, :string
  end

This model can be used just like a regular model based on a table, except it will never be saved to the database.

Vitals

Repository http://svn.viney.net.nz/things/rails/plugins/active_record_base_without_table/
License Rails' (MIT)
Rating (46 votes)
Owner Jonathan Viney
Created 4 November 2006

Comments

  • Avatar
    Maksim
    17 January 2007

    Hi! I tried installing your plugin into my rails application only to find out that it does nothing. Is it incomplete, or perhaps I got something wrong?

  • Avatar
    6 February 2007

    Wow - this BaseWithoutTable plugin business really cleans up email form validation!

    If anyone is interested in an example, I knocked up a quick <a href="http://www.kangarooit.com/developer_blog/2007/02/email-form-validation-in-ruby-on-rails.php">form validation with BaseWithoutTable tutorial</a>

  • Avatar
    Marco
    27 February 2007

    Can u post your example, please?

  • Avatar
    Frank
    5 March 2007

    Could it be that "after methods" like after_save are not be called?

  • polypus
    13 March 2007

    interesting plugin, but please change the name to TablelessModels or something. calling it active record will confuse people.

  • Avatar
    24 July 2007

    Hello, I tried to install the plugin but I continuously get a timeout on the repository address mentioned above.

    Is there an other location where I could find that plugin?

  • Avatar
    8 August 2007

    Thanks a lot! This plugin works just perfectly! This is Exactly what I needed and it was really easy to install (first time i got it done so easily with rails plugins). This is even though I am sitting under heavy company firewall and had to copy all files manually :) Great job!

  • Avatar
    8 August 2007

    This doesn't seem to work with the id ? I tried to use this in a test as an abstract model that doesn't require a model in the application, and when I tested against the id it completely failed. It just gave me nil.

  • Avatar
    Ede
    15 August 2007

    I've had a problem with this plugin if a named my model-classes different to the file name where the classes were nested. To solve this, just add

    require "filename"

    to your environment.rb and all classes in this file will be available in Rails as models without table.

  • Avatar
    20 November 2007

    awesome plugin works like a charm

  • 7 January 2008

    This plugin got broke on Rails 2. To fix, you must alter 'readmethods' to 'generatedmethods' on line 21 on basewithouttable.rb BTW, great job!

  • Avatar
    23 January 2008

    The fix is easy, but if you don't want to make it yourself, I've forked this into a Git repo and made it:

    http://gitorious.org/projects/activerecord-basewithouttable-for-rails-2

  • Avatar
    Brandon Taylor
    11 February 2008

    Wow. When I emailed the author of this plugin for help, after it didn't work, following the instructions VERBATIM. He directed me to a Rails mailing list. Not cool. 0 out of 5 stars for me.

  • Avatar
    Ben
    30 April 2008

    Love the plugin, but inheritance is broken. A column defined in the base class is not visible from a subclass. Anyone have a fix?

  • miké
    24 May 2008

    The problem is that it doesn't work with inheritance. Any idea ?

  • Avatar
    Chad
    19 June 2008

    Ah. As Vinicius noted, this is broken for Rails 2. However, there was a typo in his fix.

    readmethods must be changed to generated_methods (with an underscore)

    Do not forget to restart your server after you make the fix!

    This is a useful module. Why not apply this fix to the repository for everyone's benefit? (well, rails 2 users anyway).

  • Avatar
    Paul
    30 July 2008

    For anyone using this and trying to use a date_select, and hitting this problem

    http://dev.rubyonrails.org/ticket/10352

    Here's a workaround, that doesn't involve hacking the rails core or the plugin. It's all to do with rails needing to ask what the class (klass) of a column is, and it looks up the database type. Seeing as we don't have a table, we bork out.

    In your model that inherits from BaseWithoutTable, override the 'columnforattribute' method, and add exceptions for the fields you are using with 'date_select'. Here I am using the 'dob' field

    def columnforattribute(attribute) if (attribute.to_s.eql? 'dob') Date else super(attribute) end end

    This returns a date class when we ask for the column for 'dob'.

    This isn't the whole story, however, as rails then calls the 'klass' method on this class (as I think it's expecting an AR 'column' object)

    So somewhere in your project (I always put my extensions to the Date class in the lib folder somewhere), you need to define

    def Date.klass Date end

    Then everything should work fine.

    I'm sure someone out there's got a better engineered workaround. But I hope this gets someone out of a hole.

    Maybe I'll look to see if I can plug this hole by modifying the plugin...

  • Csongor G.
    13 August 2008

    Not trying to be too finicky but the fix for Rails 2.x is actually replacing readmethods (not readmethods) with generatedmethods

  • Avatar
    Raven
    21 January 2009

    This looks good but is it maintained enought for production? Will it work with rails 2.0.2 or is that to new? Thanks.

Add a comment