I''m working on a survey app with two possible user models: 1. The survey requires authentication. Users are created by an admin with first, last name and login and invited via an email. Personal information is taken at the end of the survey, at which point the user record is updated. 2. The survey is open. Users fill out the survey, then enter their information together with login, email, etc. User is created with all information at once. My validation looks like this: class User < ActiveRecord::Base # ... some functions # create validations validates_presence_of :login , :on => :create, :message => "Please enter a login." validates_presence_of :first_name, :on=>:create, :message => "First name is required." validates_presence_of :last_name, :on=>:create, :message => "Last name is required." validates_presence_of :email, :on=>:create, :message => "Email address is required." validates_length_of :login, :within => 3..40, :on=>:create, :message => "Login must be between 3 and 40 characters." validates_uniqueness_of :login, :on => :create, :message => "The username entered is already in use. Please try again." validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :on => :create, :message => "Please enter a valid email address." # update validations validates_inclusion_of :age, :in=>0..99, :on => :update, :message => "Age must be between 0 and 99." validates_inclusion_of :sex, :in=>%w( M F ), :on => :update, :message => "Please specify your sex." end Now consider the case where the survey is open: the validation for age and sex will not go through when they fill out the info, because it''s specific to the "update" method. On the other hand, if the :on parameter is changed to "create" the app will complain when users are being created by the admin. Has anyone else dealt with a similar situation? ___________________ Ben Jackson Diretor de Desenvolvimento ben-p14LI7ZcAE/pVLaUnt/cCQC/G2K4zDHf@public.gmane.org http://www.incomumdesign.com
As of the most recent SVN version of Rails, you can now do this: def open_survey # Return true or false depending on the state of your model end validates_inclusion_of :age, :in=>0..99, :on => :update, :message => "Age must be between 0 and 99.", :if => :open_survey validates_inclusion_of :sex, :in=>%w( M F ), :on => :update, :message => "Please specify your sex.", :if => :open_survey See ticket http://dev.rubyonrails.com/ticket/1324 Hope that helps, Duane Johnson (canadaduane) On May 28, 2005, at 4:02 PM, Ben Jackson wrote:> I''m working on a survey app with two possible user models: > > 1. The survey requires authentication. Users are created by an > admin with first, last name and login and invited via an email. > Personal information is taken at the end of the survey, at which > point the user record is updated. > 2. The survey is open. Users fill out the survey, then enter their > information together with login, email, etc. User is created with > all information at once. > > My validation looks like this: > > class User < ActiveRecord::Base > > # ... some functions > > # create validations > validates_presence_of :login , :on => :create, :message => "Please > enter a login." > validates_presence_of :first_name, :on=>:create, :message => "First > name is required." > validates_presence_of :last_name, :on=>:create, :message => "Last > name is required." > validates_presence_of :email, :on=>:create, :message => "Email > address is required." > validates_length_of :login, :within => > 3..40, :on=>:create, :message => "Login must be between 3 and 40 > characters." > validates_uniqueness_of :login, :on => :create, :message => "The > username entered is already in use. Please try again." > validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+ > [a-z]{2,})$/, :on => :create, :message => "Please enter a valid > email address." > # update validations > validates_inclusion_of :age, :in=>0..99, :on => :update, :message > => "Age must be between 0 and 99." > validates_inclusion_of :sex, :in=>%w( M F ), :on > => :update, :message => "Please specify your sex." > > end > > Now consider the case where the survey is open: the validation for > age and sex will not go through when they fill out the info, > because it''s specific to the "update" method. On the other hand, if > the :on parameter is changed to "create" the app will complain when > users are being created by the admin. > > Has anyone else dealt with a similar situation? > ___________________ > Ben Jackson > Diretor de Desenvolvimento > > ben-p14LI7ZcAE/pVLaUnt/cCQC/G2K4zDHf@public.gmane.org > http://www.incomumdesign.com > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 5/29/05, Ben Jackson <ben-p14LI7ZcAE/pVLaUnt/cCQC/G2K4zDHf@public.gmane.org> wrote:> I''m working on a survey app with two possible user models: > > 1. The survey requires authentication. Users are created by an admin > with first, last name and login and invited via an email. Personal > information is taken at the end of the survey, at which point the user > record is updated. > 2. The survey is open. Users fill out the survey, then enter their > information together with login, email, etc. User is created with all > information at once. > > My validation looks like this: > > class User < ActiveRecord::Base > > # ... some functions > > # create validations > validates_presence_of :login , :on => :create, :message => "Please > enter a login." > validates_presence_of :first_name, :on=>:create, :message => "First > name is required." > validates_presence_of :last_name, :on=>:create, :message => "Last name > is required." > validates_presence_of :email, :on=>:create, :message => "Email address > is required." > validates_length_of :login, :within => 3..40, :on=>:create, :message => > "Login must be between 3 and 40 characters." > validates_uniqueness_of :login, :on => :create, :message => "The > username entered is already in use. Please try again." > validates_format_of :email, :with => > /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :on => :create, :message => > "Please enter a valid email address." > # update validations > validates_inclusion_of :age, :in=>0..99, :on => :update, :message => > "Age must be between 0 and 99." > validates_inclusion_of :sex, :in=>%w( M F ), :on => :update, :message > => "Please specify your sex." > > end > > Now consider the case where the survey is open: the validation for age > and sex will not go through when they fill out the info, because it''s > specific to the "update" method. On the other hand, if the :on > parameter is changed to "create" the app will complain when users are > being created by the admin. > > Has anyone else dealt with a similar situation?Yes, Perhaps you want to seperate out User from Person class Person < ActiveRecord::Base has_one :user validates_presence_of :first_name, :on=>:create, :message => "First name is required." validates_presence_of :last_name, :on=>:create, :message => "Last name is required." validates_presence_of :email, :on=>:create, :message => "Email address is required." validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :on => :create, :message => "Please enter a valid email address." # update validations validates_inclusion_of :age, :in=>0..99, :on => :update, :message => "Age must be between 0 and 99." validates_inclusion_of :sex, :in=>%w( M F ), :on => :update, :message => "Please specify your sex." end class User < ActiveRecord::Base belongs_to :person validates_length_of :login, :within => 3..40, :on=>:create, :message => "Login must be between 3 and 40 characters." validates_uniqueness_of :login, :on => :create, :message => "The username entered is already in use. Please try again." validates_presence_of :login , :on => :create, :message => "Please enter a login." end Then it''s a Person which holds all the information within a survey and a User which does the logging in.> ___________________ > Ben Jackson > Diretor de Desenvolvimento > > ben-p14LI7ZcAE/pVLaUnt/cCQC/G2K4zDHf@public.gmane.org > http://www.incomumdesign.com > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
Sounds good. I installed rails with gem when I first started... how do I update to the latest commit from svn? - B On 28 May 2005, at 19:23, Duane Johnson wrote:> As of the most recent SVN version of Rails, you can now do this: > > def open_survey > # Return true or false depending on the state of your model > end > > validates_inclusion_of :age, :in=>0..99, :on => :update, :message => > "Age must be between 0 and 99.", :if => :open_survey > validates_inclusion_of :sex, :in=>%w( M F ), :on => :update, :message > => "Please specify your sex.", :if => :open_survey > > See ticket http://dev.rubyonrails.com/ticket/1324 > > Hope that helps, > Duane Johnson > (canadaduane) > > On May 28, 2005, at 4:02 PM, Ben Jackson wrote: > >> I''m working on a survey app with two possible user models: >> >> 1. The survey requires authentication. Users are created by an admin >> with first, last name and login and invited via an email. Personal >> information is taken at the end of the survey, at which point the >> user record is updated. >> 2. The survey is open. Users fill out the survey, then enter their >> information together with login, email, etc. User is created with all >> information at once. >> >> My validation looks like this: >> >> class User < ActiveRecord::Base >> >> # ... some functions >> >> # create validations >> validates_presence_of :login , :on => :create, :message => "Please >> enter a login." >> validates_presence_of :first_name, :on=>:create, :message => "First >> name is required." >> validates_presence_of :last_name, :on=>:create, :message => "Last >> name is required." >> validates_presence_of :email, :on=>:create, :message => "Email >> address is required." >> validates_length_of :login, :within => 3..40, :on=>:create, :message >> => "Login must be between 3 and 40 characters." >> validates_uniqueness_of :login, :on => :create, :message => "The >> username entered is already in use. Please try again." >> validates_format_of :email, :with => >> /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :on => :create, :message >> => "Please enter a valid email address." >> # update validations >> validates_inclusion_of :age, :in=>0..99, :on => :update, :message => >> "Age must be between 0 and 99." >> validates_inclusion_of :sex, :in=>%w( M F ), :on => :update, :message >> => "Please specify your sex." >> >> end >> >> Now consider the case where the survey is open: the validation for >> age and sex will not go through when they fill out the info, because >> it''s specific to the "update" method. On the other hand, if the :on >> parameter is changed to "create" the app will complain when users are >> being created by the admin. >> >> Has anyone else dealt with a similar situation? >> ___________________ >> Ben Jackson >> Diretor de Desenvolvimento >> >> ben-p14LI7ZcAE/pVLaUnt/cCQC/G2K4zDHf@public.gmane.org >> http://www.incomumdesign.com >> >> _______________________________________________ >> 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 >
On May 29, 2005, at 12:17 PM, Ben Jackson wrote:> Sounds good. I installed rails with gem when I first started... how > do I update to the latest commit from svn?You can move from Gem rails to what they call "edge rails" (the SVN version) by following these directions: http://wiki.rubyonrails.com/rails/show/EdgeRails The way my app is set up, it is a regular SVN project with the svn:externals tag set (as described in the link) so that on an "svn update", subversion goes out to fetch the latest rails updates as well. Duane Johnson (canadaduane) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails