Links
ActiveRecord::Base Without Table
Categories
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) |
| Tags |
activerecord class sssss
|
| Rating | (44 votes) |
| Owner | Jonathan Viney |
| Created | 4 November 2006 |
Comments
-
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?
-
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>
-
Can u post your example, please?
-
Could it be that "after methods" like after_save are not be called?
-
interesting plugin, but please change the name to TablelessModels or something. calling it active record will confuse people.
-
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?
-
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!
-
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.
-
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.
-
awesome plugin works like a charm
-
This plugin got broke on Rails 2. To fix, you must alter 'read_methods' to 'generated_methods' on line 21 on base_without_table.rb BTW, great job!
-
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
-
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.
-
Love the plugin, but inheritance is broken. A column defined in the base class is not visible from a subclass. Anyone have a fix?
-
The problem is that it doesn't work with inheritance. Any idea ?
-
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).
-
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 'column_for_attribute' method, and add exceptions for the fields you are using with 'date_select'. Here I am using the 'dob' field
def column_for_attribute(attribute)
if (attribute.to_s.eql? 'dob') Date else super(attribute) endend
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...
-
Not trying to be too finicky but the fix for Rails 2.x is actually replacing read_methods (not readmethods) with generated_methods
-
This looks good but is it maintained enought for production? Will it work with rails 2.0.2 or is that to new? Thanks.

