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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 9/27/07, Daniel Lautenschleger <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Lautenschleger 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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Lautenschleger
2007-Sep-27 20:36 UTC
Re: validates_uniqueness_of and Multiple Columns
<sigh> Bummer, but thanks for the tip. -Dan Mike Perham 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 Lautenschleger 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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sep 27, 2007, at 3:35 PM, Daniel Lautenschleger 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. > -Danvalidates_uniqueness_of :first_name, :scope => :last_name -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Lautenschleger
2007-Sep-27 20:52 UTC
Re: validates_uniqueness_of and Multiple Columns
Hey, that seems to do the trick. Thanks! -Dan Bob Showalter wrote:> On 9/27/07, Daniel Lautenschleger <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > 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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Lautenschleger
2007-Sep-27 21:00 UTC
Re: validates_uniqueness_of and Multiple Columns
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 Showalter wrote:> On 9/27/07, Daniel Lautenschleger <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > 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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 9/27/07, Bob Showalter <showaltb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 9/27/07, Daniel Lautenschleger <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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.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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Use the :message parameter: :scope => ..., :message => ''is already in our database. Please enter a unique value.'' mike Daniel Lautenschleger 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. >-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---