There may be a better approach. I have a number of forms that gather data, email the information, then throw the information out. Persistence in a database is not required. The MVC aspect of Rails requires an "M." I created a model, quote.rb, and a controller QuoteController with model :quote in the class definition. But if I try to derive class Quote from ActiveRecord::Base, it''s disallowed. I had hoped to validate input and use some of the html helpers but that does presume a database being present, right? Is there a best practice for handling this kind of thing? Thanks
You could use an in-memory sqlite database. On 10/7/05, Steve Ross <sross-ju+vs0qJmycyYsO2DCxJlVaTQe2KTcn/@public.gmane.org> wrote:> There may be a better approach. I have a number of forms that gather data, > email the information, then throw the information out. Persistence in a > database is not required. The MVC aspect of Rails requires an "M." > > I created a model, quote.rb, and a controller QuoteController with > > model :quote > > in the class definition. But if I try to derive class Quote from > ActiveRecord::Base, it''s disallowed. I had hoped to validate input and use > some of the html helpers but that does presume a database being present, > right? > > Is there a best practice for handling this kind of thing? > > Thanks > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I''ve been giving this exact same problem some thought.
And decided to come up with a "Tableless" class that extends
ActiveRecord::Base.
Here''s the code I''ve got so far...
It basically overrides the "columns" method so it doesn''t has
to go
looking for the database schema, and provide our own way to define
the "schema". And then just override all methods that do anything on
the db.
module ActiveRecord
class Tableless < ActiveRecord::Base
class <<self
def fields
@fields ||= []
end
def attribute(name, type = :string, default = nil)
@fields ||= []
@fields << ActiveRecord::ConnectionAdapters::Column.new
(name, default, type)
end
def string_attr(name, default = "")
attribute name, :string
end
end
# Quack, Quack, Quack
def self.find ; end
def self.exists? ; end
def self.create(attr = nil) ; end
def self.update(id, attr = nil) ; end
def self.delete(id) ; end
def self.destroy(id) ; end
def self.update_all(updates, conditions = nil) ; end
def self.destroy_all(conditions = nil) ; end
def self.delete_all(conditions = nil) ; end
def self.count(conditions = nil, joins = nil) 0; end
def save ; end
# We redefine this method just because the original uses
connection.columns instead of self.class.columns
def attributes_from_column_definition
self.class.columns(self.class.table_name, "#{self.class.name}
Columns").inject({}) do |attributes, column|
attributes[column.name] = column.default unless column.name
== self.class.primary_key
attributes
end
end
def self.columns(table, *etc)
return self.fields
end
end
end
So you could create a model like
class MyModel > ActiveRecord::Tableless
string_attr :name
string_attr :email
string_attr :comment
attribute :created_on, :date
end
I haven''t had time to test this code as much as I''d like... it
was
just an experiment trying to solve the problem, but for other reasons
I had to push it aside for a while.
So consider this code as a guide, rather than an implementation.
On Oct 7, 2005, at 4:53 PM, Steve Ross wrote:
> There may be a better approach. I have a number of forms that
> gather data,
> email the information, then throw the information out. Persistence
> in a
> database is not required. The MVC aspect of Rails requires an
"M."
>
> I created a model, quote.rb, and a controller QuoteController with
>
> model :quote
>
> in the class definition. But if I try to derive class Quote from
> ActiveRecord::Base, it''s disallowed. I had hoped to validate input
> and use
> some of the html helpers but that does presume a database being
> present,
> right?
>
> Is there a best practice for handling this kind of thing?
>
> Thanks
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
On 8 Oct 2005, at 14:57, Sebastián Delmont wrote:> I''ve been giving this exact same problem some thought. > > And decided to come up with a "Tableless" class that extends > ActiveRecord::Base.Woah.> Here''s the code I''ve got so far... > > It basically overrides the "columns" method so it doesn''t has to go > looking for the database schema, and provide our own way to define the > "schema". And then just override all methods that do anything on the > db.This might well be overkill, if all you need to do is interact with the model fields. You can let the form define the schema - if it tries to set a field, create that field. You aren''t forced to interact with your models via an AR-like API - you just need to do the minimal stuff that the model fields need, and make it possible to initialize your model from the params hash. It has been pointed out on this list that you can probably get most of what you want with a standard ruby OpenStruct (note, I''m not using this code myself, but it should work). Stick this in lib/, require it with require_dependency, this way all your subclasses will reload in development. class SimpleModel < OpenStruct Dependencies.remove_subclasses_for(SimpleModel) if defined?(SimpleModel) def logger RAILS_DEFAULT_LOGGER end end Personally because I want some hash functionality with my non-AR models, I use one based directly off a hash: class SimpleModel < Hash Dependencies.remove_subclasses_for(SimpleModel) if defined?(SimpleModel) extend ModelExtras # initialize - merges in the parameters passed def initialize(hash=nil) self.merge!(hash) if hash end def logger RAILS_DEFAULT_LOGGER end # method missing - simple map to hash key with same name def method_missing(method, *args) method = method.to_s if method =~ /^(.*)=$/ self[$1] = (args.length > 1 ? args : args.first) else self[method] end end end This is probably a good topic for a wiki page. I''m not sure if I''m missing any other duck-typing that I would need to cater for, for standard model uses; this works for me though - I''m using model helpers, and indeed storing these in the session (and in a serialized DB column; it''s a long story). Mike
On 10/8/05, Michael Houghton <mike-GDRLylaSUshUrdklU0bPTip2UmYkHbXO@public.gmane.org> wrote:> On 8 Oct 2005, at 14:57, Sebastián Delmont wrote: > > > I''ve been giving this exact same problem some thought. > > > > And decided to come up with a "Tableless" class that extends > > ActiveRecord::Base.Here''s what I did for a recent project: http://www.bigbold.com/snippets/posts/show/767 I didn''t feel like setting up a testing framework with all the rails DBs (similar to the ActiveRecord test suite), so I mocked a few things and the tests still passed. I''d like to mock find() so it uses fixtures if available, but I may just go back to using plain old AR. -- rick http://techno-weenie.net
Both these solutions (actually *all* of them) look great, but I''m still
having trouble with the ActionView::Helpers.
Code:
<p>*Name: <%= input(''amu_quote'',
''name'') %></p>
The error message I get is
undefined method `column_for_attribute'' for true:TrueClass''
This appears to be a result of there being no way for ActiveRecordHelper to
determine the column type. Any further thoughts?
Thx
-----Original Message-----
From: Rick Olson [mailto:technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org]
Sent: Saturday, October 08, 2005 8:49 AM
To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails] ActiveRecord with No Database
On 10/8/05, Michael Houghton
<mike-GDRLylaSUshUrdklU0bPTip2UmYkHbXO@public.gmane.org>
wrote:> On 8 Oct 2005, at 14:57, Sebastián Delmont wrote:
>
> > I''ve been giving this exact same problem some thought.
> >
> > And decided to come up with a "Tableless" class that extends
> > ActiveRecord::Base.
Here''s what I did for a recent project:
http://www.bigbold.com/snippets/posts/show/767
I didn''t feel like setting up a testing framework with all the rails
DBs (similar to the ActiveRecord test suite), so I mocked a few things
and the tests still passed. I''d like to mock find() so it uses
fixtures if available, but I may just go back to using plain old AR.
--
rick
http://techno-weenie.net
Have you seen and read this : http://wiki.rubyonrails.com/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord I think it''s quite a good solution to the problem. (except that validates_numericality_of fails but you can use validates_format_of with a regex for numbers) Jean On 10/8/05, Steve Ross (nphoto) <sross-87aD/6bQ6cTQT0dZR+AlfA@public.gmane.org> wrote:> Both these solutions (actually *all* of them) look great, but I''m still > having trouble with the ActionView::Helpers. > > Code: > > <p>*Name: <%= input(''amu_quote'', ''name'') %></p> > > The error message I get is > > undefined method `column_for_attribute'' for true:TrueClass'' > > This appears to be a result of there being no way for ActiveRecordHelper to > determine the column type. Any further thoughts? > > Thx > > -----Original Message----- > From: Rick Olson [mailto:technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org] > Sent: Saturday, October 08, 2005 8:49 AM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails] ActiveRecord with No Database > > On 10/8/05, Michael Houghton <mike-GDRLylaSUshUrdklU0bPTip2UmYkHbXO@public.gmane.org> wrote: > > On 8 Oct 2005, at 14:57, Sebastián Delmont wrote: > > > > > I''ve been giving this exact same problem some thought. > > > > > > And decided to come up with a "Tableless" class that extends > > > ActiveRecord::Base. > > Here''s what I did for a recent project: > http://www.bigbold.com/snippets/posts/show/767 > > I didn''t feel like setting up a testing framework with all the rails > DBs (similar to the ActiveRecord test suite), so I mocked a few things > and the tests still passed. I''d like to mock find() so it uses > fixtures if available, but I may just go back to using plain old AR. > > > -- > rick > http://techno-weenie.net > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Steve: Ugh, this will be me missing the point of the original post again - oops, sorry! I guess if you want to use that method then my code/the openstruct stuff won''t help. I use the specific field helpers for the type I want, and they clearly are not calling that. Evidently I need to go back to the drawing board here. Jean: thanks for the link, that looks interesting. I haven''t had cause to put validations on my transient models yet, but I might. Excellent. Mike On 9 Oct 2005, at 00:00, Jean Helou wrote:> Have you seen and read this : > http://wiki.rubyonrails.com/rails/pages/ > HowToUseValidationsWithoutExtendingActiveRecord > > I think it''s quite a good solution to the problem. (except that > validates_numericality_of fails but you can use validates_format_of > with a regex for numbers) > > Jean > > On 10/8/05, Steve Ross (nphoto) <sross-87aD/6bQ6cTQT0dZR+AlfA@public.gmane.org> wrote: >> Both these solutions (actually *all* of them) look great, but I''m >> still >> having trouble with the ActionView::Helpers. >> >> Code: >> >> <p>*Name: <%= input(''amu_quote'', ''name'') %></p> >> >> The error message I get is >> >> undefined method `column_for_attribute'' for true:TrueClass'' >> >> This appears to be a result of there being no way for >> ActiveRecordHelper to >> determine the column type. Any further thoughts? >> >> Thx >> >> -----Original Message----- >> From: Rick Olson [mailto:technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org] >> Sent: Saturday, October 08, 2005 8:49 AM >> To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> Subject: Re: [Rails] ActiveRecord with No Database >> >> On 10/8/05, Michael Houghton <mike-GDRLylaSUshUrdklU0bPTip2UmYkHbXO@public.gmane.org> wrote: >>> On 8 Oct 2005, at 14:57, Sebastián Delmont wrote: >>> >>>> I''ve been giving this exact same problem some thought. >>>> >>>> And decided to come up with a "Tableless" class that extends >>>> ActiveRecord::Base. >> >> Here''s what I did for a recent project: >> http://www.bigbold.com/snippets/posts/show/767 >> >> I didn''t feel like setting up a testing framework with all the rails >> DBs (similar to the ActiveRecord test suite), so I mocked a few things >> and the tests still passed. I''d like to mock find() so it uses >> fixtures if available, but I may just go back to using plain old AR. >> >> >> -- >> rick >> http://techno-weenie.net >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >