views:

189

answers:

2

I am trying to create an ActiveRecord model called 'Search' without a table. I keep getting this error when I do @search = Search.new.

sql::Error: Table 'searchdemo_development.tablelesses' doesn't exist: SELECT * FROM tablelesses

I am using the idea from this comment: http://stackoverflow.com/questions/315850/rails-model-without-database/318919#318919. I also get the same kind of error doing the basic:

class Search < ActiveRecord::Base

end

How do I get ActiveRecord stop looking for a table?

+2  A: 

Why in the name of the lord would you want an actve record model without a table? The purpose of an active record model is to communicate with a database. I assume that it is impossible to have an active record model without a db table.

Perhaps you want a regular class?

class Search
  # your methods here
end
August Lilleaas
I agree that it seems a strange use of the ActiveRecord pattern to have a class without a table, but in fact the validations provided by Rails' implementation of ActiveRecord are still useful for model data that you may not want to persist. And it's certainly not impossible to do.
John Topley
If you want validations, there's a gem for that which is not related to Rails or Active Record. I don't rememeber the name, but I guess you'll find it on rubyforge. Or you can write your own ; ) http://stackoverflow.com/questions/775976
August Lilleaas
Oh, and hopefully this will change in Rails 3.0, with ActiveModel. `include ActiveModel::Validations`, have lunch. =D
August Lilleaas
A: 

I can think of a few reasons you might want to do something like this. Perhaps you want to leverage some of the non-db-related methods on ActiveRecord or you want to pass your object to something that expects and ActiveRecord instance. Without more info, it is impossible to say whether the choice to use AR here is correct or incorrect.

In any event, if you want to continue on this path...

check out this Railscast http://railscasts.com/episodes/121-non-active-record-model

and also checkout this gem: http://github.com/kennethkalmer/activerecord-tableless-models/tree/master

semanticart
What I'm trying to do is similar to http://railscasts.com/episodes/111-advanced-search-form except I don't want to save searches to a database or even have a table for the model.
thaiyoshi