Hi, Forword : I know of the technique described in http://wiki.rubyonrails.com/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord but I find it quite lacking (numericality validation fails, reloading too) I want to be able to use a model object for forms (initial values, validation, etc) but for data which will not be saved as is in my database.A friend found another way than the technique mentioned. He created a table in the db, a model for it and abused active record into not saving the data in the database: a object extending private def create_or_update_without_callbacks true end however I think this is pretty ugly ... I don''t like the idea of having lots of empty tables lying in the database. I have also heard of a way which uses an OpenStruct though I haven''t seen any examples of validation with it ... What is The Enlightened Right Thing to do ? jean
I have the same need too... there must be a way of solving this as it seems to me like a pretty general use case. Django uses Custom Manipulators to achieve this <http://www.djangoproject.com/documentation/forms/>. Let me know if you find a solution in Rails, Dado Jean Helou wrote:> Hi, > > Forword : > I know of the technique described in >http://wiki.rubyonrails.com/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord> but I find it quite lacking (numericality validation fails, reloading > too) > > I want to be able to use a model object for forms (initial values, > validation, etc) but for data which will not be saved as is in my > database.A friend found another way than the technique mentioned. He > created a table in the db, a model for it and abused active record > into not saving the data in the database: > > a object extending > private > def create_or_update_without_callbacks > true > end > > however I think this is pretty ugly ... I don''t like the idea of > having lots of empty tables lying in the database. > > I have also heard of a way which uses an OpenStruct though I haven''t > seen any examples of validation with it ... > > What is The Enlightened Right Thing to do ? > > jean
Jean Helou wrote:> Hi, > > Forword : > I know of the technique described in > http://wiki.rubyonrails.com/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord > but I find it quite lacking (numericality validation fails, reloading > too) > > I want to be able to use a model object for forms (initial values, > validation, etc) but for data which will not be saved as is in my > database.A friend found another way than the technique mentioned. He > created a table in the db, a model for it and abused active record > into not saving the data in the database: > > a object extending > private > def create_or_update_without_callbacks > true > end > > however I think this is pretty ugly ... I don''t like the idea of > having lots of empty tables lying in the database. > > I have also heard of a way which uses an OpenStruct though I haven''t > seen any examples of validation with it ... > > What is The Enlightened Right Thing to do ?I don''t know about enlightened, but here''s what I did. I built a table in an in-memory sqlite database, so it never gets saved to permanent storage, but I get all the AR goodies. It creates the database and tables the first time the model is loaded: # There are no records that are stored in a database for the model, so # build a memory based Sqlite database to build a schema to help build # an ActiveRecord. # # Because no objects instances of this model are stored in the # database, conventional usage of an object of this model is to have # the object''s id be the same as the output type''s id so linking them # up is easy. class Post < ActiveRecord::Base unless const_defined? :SQLITE_SETUP SQLITE_SETUP = true self.establish_connection(:adapter => ''sqlite'', :dbfile => '':memory:'') conn = self.connection conn.create_table(self.table_name, :primary_key => self.primary_key) do |t| t.column :count, :integer end end attr_accessible :count validates_numericality_of :count, :only_integer => true end Regards, Blair -- Blair Zajac, Ph.D. <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org> Subversion and Orca training and consulting http://www.orcaware.com/svn/
On 11/1/05, Blair Zajac <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org> wrote: [...]> I don''t know about enlightened, but here''s what I did. I built a table in an > in-memory sqlite database, so it never gets saved to permanent storage, but I > get all the AR goodies. It creates the database and tables the first time the > model is loaded: > > # There are no records that are stored in a database for the model, so > # build a memory based Sqlite database to build a schema to help build > # an ActiveRecord. > # > # Because no objects instances of this model are stored in the > # database, conventional usage of an object of this model is to have > # the object''s id be the same as the output type''s id so linking them > # up is easy. > > class Post < ActiveRecord::Base > > unless const_defined? :SQLITE_SETUP > SQLITE_SETUP = true > self.establish_connection(:adapter => ''sqlite'', :dbfile => '':memory:'') > conn = self.connection > conn.create_table(self.table_name, > :primary_key => self.primary_key) do |t| > t.column :count, :integer > end > end > > attr_accessible :count > validates_numericality_of :count, :only_integer => true > end > > Regards, > Blair > > -- > Blair Zajac, Ph.D. > <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org>Thank you Blair, this solution is cleaner and faster than my second one (abusing active record), but I still find it overkill to use a relational table for field definition. I will continue working on this, there must be a way of doing it cleanly in ruby. Regards, Jean