Forum: Ruby on Rails Validations for non-ActiveRecord Model Objects

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
67b6389be42524fbd776e44fd35c3d7e?d=identicon&s=25 peter.j.donald (Guest)
on 2005-12-02 16:19
(Received via mailing list)
Hi,

I just worked on getting validations for non-ActiveRecord model
objects working. I essentially implemented an ActiveForm object that
you can subclass for your own non-database form objects. The file is
available from my blog [1] and I wouldn't mind some feedback on
whether my assumptions are right. It seems to work for me and makes it
much easier to write non-database bound forms.

[1]
http://www.realityforge.org/articles/2005/12/02/va...

--
Cheers,

Peter Donald
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 technoweenie (Guest)
on 2005-12-02 17:41
(Received via mailing list)
On 12/2/05, Peter Donald <peter.j.donald@gmail.com> wrote:
>
> --
> Cheers,
>
> Peter Donald

Here's my approach to the problem.  Instead of making my own class and
reimplementing the validations, I just removed the code that grabs the
table info:

class Contact < ActiveRecord::Base
  def self.columns() @columns ||= []; end
  def self.column(name, sql_type = nil, default = nil, null = true)
    columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s,
default, sql_type.to_s, null)
  end

  column :name,          :string
  column :city,          :string
  column :state,         :string
  column :phone,         :string
  column :email_address, :string

  validates_presence_of :name, :email_address
end

It's still technically a model, so you can call #save on it.  But just
calling valid? works fine.

http://rails.techno-weenie.net/tip/2005/11/19/vali...


--
rick
http://techno-weenie.net
67b6389be42524fbd776e44fd35c3d7e?d=identicon&s=25 peter.j.donald (Guest)
on 2005-12-06 04:13
(Received via mailing list)
Hi,

It turns out that there is a wiki page[1] that already describes this
technique. Who knew? :) It is a little dated but I wish I had wiki'ed
before attempting this ;)

[1]
http://wiki.rubyonrails.com/rails/pages/HowToUseVa...


On 12/3/05, Peter Donald <peter.j.donald@gmail.com> wrote:
>
> --
> Cheers,
>
> Peter Donald
>


--
Cheers,

Peter Donald
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 technoweenie (Guest)
on 2005-12-06 05:30
(Received via mailing list)
On 12/5/05, Peter Donald <peter.j.donald@gmail.com> wrote:
> Hi,
>
> It turns out that there is a wiki page[1] that already describes this
> technique. Who knew? :) It is a little dated but I wish I had wiki'ed
> before attempting this ;)
>
> [1] 
http://wiki.rubyonrails.com/rails/pages/HowToUseVa...

I knew about that, but my version doesn't mess with rails as much. I
prefer my 3 lines to set it up vs changes in the dark corners of the
dispatcher libs.  YMMV


--
rick
http://techno-weenie.net
This topic is locked and can not be replied to.