Yep, a newbie
I first_name and last_name in a table. I’d like to know how I can use
validates_uniqueness_of to make sure the combination of the two only
appears once in my database.
Thanks a ton.
-Dan
Yep, a newbie
I first_name and last_name in a table. I’d like to know how I can use
validates_uniqueness_of to make sure the combination of the two only
appears once in my database.
Thanks a ton.
-Dan
vuo will accept multiple attribute names but checks them individually,
not as an AND. You’d have to roll your own validation method. Look
into the ActiveRecord validation docs on how to do that.
mike
Daniel L. wrote:
I first_name and last_name in a table. I’d like to know how I can use
validates_uniqueness_of to make sure the combination of the two only
appears once in my database.
Bummer, but thanks for the tip.
-Dan
Mike P. wrote:
vuo will accept multiple attribute names but checks them individually,
not as an AND. You’d have to roll your own validation method. Look
into the ActiveRecord validation docs on how to do that.mike
Daniel L. wrote:
I first_name and last_name in a table. I’d like to know how I can use
validates_uniqueness_of to make sure the combination of the two only
appears once in my database.
Hey, that seems to do the trick.
Thanks!
-Dan
Bob S. wrote:
On 9/27/07, Daniel L. [email protected]
wrote:Yep, a newbie
I first_name and last_name in a table. I’d like to know how I can use
validates_uniqueness_of to make sure the combination of the two only
appears once in my database.Use :scope
validates_uniqueness_of :first_name, :scope => :last_name
An index on last_name would be appropriate as well if this table is
large.
On 9/27/07, Daniel L. [email protected]
wrote:
Yep, a newbie
I first_name and last_name in a table. I’d like to know how I can use
validates_uniqueness_of to make sure the combination of the two only
appears once in my database.
Use :scope
validates_uniqueness_of :first_name, :scope => :last_name
An index on last_name would be appropriate as well if this table is
large.
One more question:
Is there a simple method of defining my own error message that’s
associated with a failed validation?
The response from the failed validation should be something like “A
person with this name already exists in the database.” rather than what
it says now.
Thanks much. I’m really enjoying Rails. I had to spend way too much time
generating validation rules in PHP not to mention the scaffolding
feature. Looks fairly easy to modify the scaffold code too by creating
static content.
-Dan
Bob S. wrote:
On 9/27/07, Daniel L. [email protected]
wrote:Yep, a newbie
I first_name and last_name in a table. I’d like to know how I can use
validates_uniqueness_of to make sure the combination of the two only
appears once in my database.Use :scope
validates_uniqueness_of :first_name, :scope => :last_name
An index on last_name would be appropriate as well if this table is
large.
Use the :message parameter:
:scope => …, :message => ‘is already in our database. Please enter a
unique value.’
mike
Daniel L. wrote:
One more question:
Is there a simple method of defining my own error message that’s
associated with a failed validation?The response from the failed validation should be something like “A
person with this name already exists in the database.” rather than what
it says now.Thanks much. I’m really enjoying Rails. I had to spend way too much time
generating validation rules in PHP not to mention the scaffolding
feature. Looks fairly easy to modify the scaffold code too by creating
static content.
On 9/27/07, Bob S. [email protected] wrote:
validates_uniqueness_of :first_name, :scope => :last_name
An index on last_name would be appropriate as well if this table is large.
I’ve never done this, but I think I would like to write a composed_of
object (FullName maybe?) and define some validation on that.
class User < ActiveRecord::Base
composed_of :full_name, :mapping => %w(first_name last_name)
def validate
errors.add_to_base(“Name must be unique”) unless full_name.unique?
end
end
I like how that expresses the intent so clearly.
Pat
On Sep 27, 2007, at 3:35 PM, Daniel L. wrote:
Yep, a newbie
I first_name and last_name in a table. I’d like to know how I can use
validates_uniqueness_of to make sure the combination of the two only
appears once in my database.Thanks a ton.
-Dan
validates_uniqueness_of :first_name, :scope => :last_name
-Rob
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs