Displaying 20 results from an estimated 7000 matches similar to: "Rails validations, how to improve them"
2009 Sep 19
1
validations on a field
hi, i have a model with 2 validations on a field
example:
validates_presence_of :email
validates_length_of :email, :within => 6..100 #r@a.wk
validates_uniqueness_of :email
validates_format_of :email, :with =>
Authentication.email_regex
if i do the submit of form for a new object of the model with a blank
mail
i receive 4 errors.
In this case i want receive
2006 Apr 14
1
Validating Existing Models
I''ve got a user object which has quite a few attributes mapped to the
database. As of right now I''ve got some validations in the model like:
validates_presence_of :password, :message => "You must specify a valid
password."
validates_confirmation_of :password, :message => "Password doesn''t match
the confirmation."
validates_presence_of
2006 Aug 14
3
validating required fields during an update?
I have a User model with an email attribute. I''m using the following
validations:
validates_confirmation_of :email
validates_format_of :email,
:with => RFC822::EmailAddress,
:message => "must be a valid address in the form user@domain.com"
validates_uniqueness_of :email, :message => "address has already
been taken. If you''ve forgotten your
2005 Nov 26
0
Model warnings along with Validations
Hi railers,
Is there a simple method to extend Active Record to have similar
functionality as the validate functions e.g.
validate_on_find
validates_acceptance_of
validates_each
validates_format_of
validates_length_of
validates_uniqueness_of
etc...
but only report warning on model attributes instead of errors().
I would like to save the model if there are warning and if the model is
invalid
2006 Jan 12
1
A really newbie question
Hi,
I have been playing around with Ruby after reading so much about it, and
so I started tweaking with auto generated scaffold.
But it seems like I must be missing something, can someone else please
help me take a look and give me a couple pointers? In short I was trying
to make a class User that has a method addupdate with 2 parameters
(alias and email) which would either update the
2007 Aug 07
2
Making Validations Optional
Hello,
I am working with URL validations. I want this validation to be
optional if the data exists in the form submission. I have tried
several things.
validates_format_of :website,
:if => params[:organization][:website],
:with => /((http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]
{2,5}(([0-9]{1,5})?\/.*)?/,
:message => "Please enter a valid url"
Of
2006 Feb 21
2
validations and directing errors
I am starting to play with validations as I want to catch errors before
SQL rejects inserts to the table which are ugly when nulls are sent to
''Not Null'' columns, etc.
If I do something in my model like...
validates_format_of :first_name,
:with => /^\w+$/,
:message => "you moron, enter a name"
I still end up
2005 Dec 29
7
belongs_to causing NoMethodError exceptions ... ?
I''ve got a really strange problem using belongs_to. I apologize in
advance for the length... this is going to take a while to explain.
Basic idea: Creating a User requires that the user enter an email
address and activation key that matches an existing PendingUser.
After creating the user successfully, that pending user should be
marked as "used".
The problem:
When I
2006 Apr 13
1
Model is not a class
I''m trying to specify a model in a controller, but rails gives me
"Account is not a class", where Account is my model name ...
This is my model:
class Account < ActiveRecord::Base
validates_presence_of :email, :email_confirmation,
:username, :password, :password_confirmation
validates_format_of :email,
:with =>
2006 Sep 12
4
ActiveRecord Validations - validates_uniqueness_of
Hello,
I''m wanting to use Mocha and Stubba for my tests in place of the
default Rails testing with fixtures.
I have the following class:
class Person < ActiveRecord::Base
validates_uniqueness_of :email
end
and am wondering how I can test to ensure that the
validates_uniqueness_of validation is running.
Any ideas?
-Jonathan
2005 Apr 20
2
Has And Belongs To Many Validations
Here is the schema that I am working with:
courses -> courses_teachers <- teachers
I want to ensure that a teacher is not assigned the same course more
than once. In other words, if I''m in the courses_teachers table as
teaching Spanish 101, if someone tries to add that same teacher/course
combination to the table again, it will not be allowed.
I thought I could use something
2006 Jun 05
2
When adding a record in console, a parameter comes in as null even when I set it
In console, I am trying to create a User but the :account_id does not come
in. Console just gives me back :account_id => nil. Obviously I''m trying to
set it though. Silly console...
But, I can set the account_id column in my controller like so:
@user = User.new(params[:user])
@user.account_id = account.id
@user.save
Here''s what I give to the controller:
User.create :name
2006 Feb 16
1
validations with associated object
Does anyone know if Rails is supposed to validate associated objects on
creation?
Say you have a User model, with has_many posts, and a Post model which
belongs_to user. Further, the Post model has a validates_uniqueness_of
:title.
u = User.find(1)
# this should pass
u.posts.create :title => ''my new post title''
# this should fail
u.posts.create :title => ''my
2006 Apr 28
2
validates* gives me problems
Hi,
In Debian/testing (mysql-5.0.20, ruby1.8, rails 1.1.0)
Im trying to follow up the fourdaysonrails tutorial, and when putting:
validates_uniqueness_of :category, :message => "Already exists"
in /myapplication/app/model/category.rb
I get:
NoMethodError in Categories#create
undefined method `category'' for #<Article:0x407f6418>
RAILS_ROOT:
2008 Feb 09
0
a problem with before_create and validations
I stumble across some strange problem.
I have a model with a virtual attribute file_contents that is a string.
The model has validates_format_of :file_contents, :on => create, :unless
=> no_contents? (The method that returns file_contents.nil?)
Then, I have a before_create callback, where I get my model''s title and
description by parsing file_contents, and do write_attribute for
2009 Jun 17
2
validates_uniqueness_of fails on STI in Rails 2.3.2
I''ve got something like this:
class Position < ActiveRecord::Base
end
class CartItem < Position
validates_uniqueness_of :product_id
end
When I try to save CartItem I get this error:
ArgumentError: wrong number of arguments (1 for 2)
from
/home/joaz/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/active_record/validations.rb:758:in
`exists?''
from
2010 Nov 11
5
ActiveRecord query
I have these sql code in postgresql
"SELECT * from convenios where id NOT IN
(SELECT convenio_id from solicituds where usuario_id=?"
but don''t find a way of used it in rails
except
find_by_sql.
There is a another way??
class Usuario < ActiveRecord::Base
belongs_to :persona
has_many :solicituds, :dependent => :destroy
has_many :convenios, :through =>
2006 Oct 05
0
ActiveRecord Validations - validates_uniqueness_of
David Chelimsky <dchelimsky at gmail.com> wrote:
> What I would really want to ensure here is that Person validates
> uniqueness of email, not how it does it. What I would do is something
> like:
>
> def test_person_should_validate_uniqueness_of_email
> assert_true Person.new(:email => "a at b.com").save
> assert_false Person.new(:email => "a at
2008 Jun 07
3
DRY validates_format_of?
In a model I have following validates_format_of. Is there an easy way to
DRY this up? It seems to be rather repetitive.
validates_format_of :expiration_date, :with =>
/^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/
validates_format_of :activation_date, :with =>
/^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/
validates_format_of :some_other_date, :with =>
2007 Jan 01
5
Validations based on associations
My model is very simple, it''s mostly just a join table that represents
which tournaments a user has registered for.
class Registration < ActiveRecord::Base
belongs_to :user
belongs_to :tournament
validates_presence_of :user_id, :tournament_id
validates_uniqueness_of :user_id, :scope => :tournament_id
end
the validates_uniqueness checks to make sure there''s no