Bill Walton
2006-Mar-26 17:23 UTC
[Rails] validate_uniqueness_of {combination of fields} ???
I''ve looked at the validates documentation and it appears that validates_uniqueness_of works on only one field at a time. So I can, for example, validate that "sam" is unique to the record set, and that "smith" is unique to the record set. My problem is that each record includes two fields, the _combination_ of which must be unique to the record set, but each of which may not be unique. So "sam smith", "mary smith" and "sam jones" (stored as fname, lname) all need to pass the validation. Is there an easy way to validate_uniqueness_of a combination of fields within a record? Thanks in advance for any help. Best regards, Bill -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060326/08cf5ae6/attachment.html
Bill Walton
2006-Mar-26 19:02 UTC
[Rails] validate_uniqueness_of {combination of fields} ???
I''ve realized in rereading my post below that I could have stated the problem I''m having more clearly. The system is multi-user, so the record set within which I''m trying to validate_uniqueness_of is constrained by a third field (e.g., ''group_id''). So I''m already using the "scope" option on validates_uniqueness_of. I think what I''m looking for is nested scoping. Something like: validates_uniqueness_of :fname, :scope => "lname", :scope => "group_id" Is there an easy (i.e., Rails) way to accomplish this? Thanks, Bill ----- Original Message ----- From: Bill Walton To: rails@lists.rubyonrails.org Sent: 2006-03-26 11:08 AM Subject: [Rails] validate_uniqueness_of {combination of fields} ??? I''ve looked at the validates documentation and it appears that validates_uniqueness_of works on only one field at a time. So I can, for example, validate that "sam" is unique to the record set, and that "smith" is unique to the record set. My problem is that each record includes two fields, the _combination_ of which must be unique to the record set, but each of which may not be unique. So "sam smith", "mary smith" and "sam jones" (stored as fname, lname) all need to pass the validation. Is there an easy way to validate_uniqueness_of a combination of fields within a record? Thanks in advance for any help. Best regards, Bill ------------------------------------------------------------------------------ _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060326/14a4406a/attachment.html
Bill Walton
2006-Mar-26 19:36 UTC
[Rails] validate_uniqueness_of {combination of fields} ???
I''ll be damned. The nested scoping works just the way I wrote it below. I''m posting this just so another newbie might be spared the angst. RoR ROCKS! ----- Original Message ----- From: Bill Walton To: rails@lists.rubyonrails.org Sent: 2006-03-26 12:47 PM Subject: Re: [Rails] validate_uniqueness_of {combination of fields} ??? I''ve realized in rereading my post below that I could have stated the problem I''m having more clearly. The system is multi-user, so the record set within which I''m trying to validate_uniqueness_of is constrained by a third field (e.g., ''group_id''). So I''m already using the "scope" option on validates_uniqueness_of. I think what I''m looking for is nested scoping. Something like: validates_uniqueness_of :fname, :scope => "lname", :scope => "group_id" Is there an easy (i.e., Rails) way to accomplish this? Thanks, Bill ----- Original Message ----- From: Bill Walton To: rails@lists.rubyonrails.org Sent: 2006-03-26 11:08 AM Subject: [Rails] validate_uniqueness_of {combination of fields} ??? I''ve looked at the validates documentation and it appears that validates_uniqueness_of works on only one field at a time. So I can, for example, validate that "sam" is unique to the record set, and that "smith" is unique to the record set. My problem is that each record includes two fields, the _combination_ of which must be unique to the record set, but each of which may not be unique. So "sam smith", "mary smith" and "sam jones" (stored as fname, lname) all need to pass the validation. Is there an easy way to validate_uniqueness_of a combination of fields within a record? Thanks in advance for any help. Best regards, Bill ---------------------------------------------------------------------------- _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails ------------------------------------------------------------------------------ _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060326/66eb4f74/attachment-0001.html
Bill Walton
2006-Mar-26 20:45 UTC
[Rails] validate_uniqueness_of {combination of fields} ???
Oops. Spoke too soon. While it doesn''t throw a syntax error. only the last ":scope => x" option is acted upon. So the question remains... Is there an easy way to accomplish nested scoping? Thanks much for any advice. Best regards, Bill ----- Original Message ----- From: Bill Walton To: rails@lists.rubyonrails.org Sent: 2006-03-26 1:21 PM Subject: Re: [Rails] validate_uniqueness_of {combination of fields} ??? I''ll be damned. The nested scoping works just the way I wrote it below. I''m posting this just so another newbie might be spared the angst. RoR ROCKS! ----- Original Message ----- From: Bill Walton To: rails@lists.rubyonrails.org Sent: 2006-03-26 12:47 PM Subject: Re: [Rails] validate_uniqueness_of {combination of fields} ??? I''ve realized in rereading my post below that I could have stated the problem I''m having more clearly. The system is multi-user, so the record set within which I''m trying to validate_uniqueness_of is constrained by a third field (e.g., ''group_id''). So I''m already using the "scope" option on validates_uniqueness_of. I think what I''m looking for is nested scoping. Something like: validates_uniqueness_of :fname, :scope => "lname", :scope => "group_id" Is there an easy (i.e., Rails) way to accomplish this? Thanks, Bill ----- Original Message ----- From: Bill Walton To: rails@lists.rubyonrails.org Sent: 2006-03-26 11:08 AM Subject: [Rails] validate_uniqueness_of {combination of fields} ??? I''ve looked at the validates documentation and it appears that validates_uniqueness_of works on only one field at a time. So I can, for example, validate that "sam" is unique to the record set, and that "smith" is unique to the record set. My problem is that each record includes two fields, the _combination_ of which must be unique to the record set, but each of which may not be unique. So "sam smith", "mary smith" and "sam jones" (stored as fname, lname) all need to pass the validation. Is there an easy way to validate_uniqueness_of a combination of fields within a record? Thanks in advance for any help. Best regards, Bill -------------------------------------------------------------------------- _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails ---------------------------------------------------------------------------- _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails ------------------------------------------------------------------------------ _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060326/df054f85/attachment-0001.html
Hi Bill, I think you can achieve what you are after by defining a method like so: def fullname [fname, lname]. join '' '' end then validate that: validates_uniqueness_of :full_name, :scope => "group_id" That should do it. Cheers, Dan On 3/26/06, Bill Walton <bill.walton@charter.net> wrote:> > Oops. Spoke too soon. While it doesn''t throw a syntax error. only the last > ":scope => x" option is acted upon. So the question remains... > > Is there an easy way to accomplish nested scoping? > > Thanks much for any advice. > > Best regards, > > Bill > > > ----- Original Message ----- > From: Bill Walton > To: rails@lists.rubyonrails.org > > Sent: 2006-03-26 1:21 PM > Subject: Re: [Rails] validate_uniqueness_of {combination of fields} ??? > > > I''ll be damned. The nested scoping works just the way I wrote it below. > I''m posting this just so another newbie might be spared the angst. RoR > ROCKS! > > ----- Original Message ----- > From: Bill Walton > To: rails@lists.rubyonrails.org > Sent: 2006-03-26 12:47 PM > Subject: Re: [Rails] validate_uniqueness_of {combination of fields} ??? > > > I''ve realized in rereading my post below that I could have stated the > problem I''m having more clearly. The system is multi-user, so the record > set within which I''m trying to validate_uniqueness_of is constrained by a > third field (e.g., ''group_id''). So I''m already using the "scope" option on > validates_uniqueness_of. I think what I''m looking for is nested scoping. > Something like: > > validates_uniqueness_of :fname, :scope => "lname", :scope => "group_id" > > Is there an easy (i.e., Rails) way to accomplish this? > > Thanks, > Bill > > > > ----- Original Message ----- > From: Bill Walton > To: rails@lists.rubyonrails.org > Sent: 2006-03-26 11:08 AM > Subject: [Rails] validate_uniqueness_of {combination of fields} ??? > > > > I''ve looked at the validates documentation and it appears that > validates_uniqueness_of works on only one field at a time. So I can, for > example, validate that "sam" is unique to the record set, and that "smith" > is unique to the record set. > > My problem is that each record includes two fields, the _combination_ of > which must be unique to the record set, but each of which may not be unique. > So "sam smith", "mary smith" and "sam jones" (stored as fname, lname) all > need to pass the validation. > > > Is there an easy way to validate_uniqueness_of a combination of fields > within a record? > > Thanks in advance for any help. > > Best regards, > Bill > > > > ________________________________ > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > ________________________________ > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > ________________________________ > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Dan Webb http://www.danwebb.net
oops, theres typos in that code. I should go to bed. Here it is again: def full_name [fname, lname].join '' '' end validates_uniqueness_of :full_name, :scope => "group_id" On 3/26/06, Dan Webb <dan@danwebb.net> wrote:> Hi Bill, > > I think you can achieve what you are after by defining a method like so: > > def fullname > [fname, lname]. join '' '' > end > > then validate that: > > validates_uniqueness_of :full_name, :scope => "group_id" > > That should do it. > > Cheers, > > Dan > > On 3/26/06, Bill Walton <bill.walton@charter.net> wrote: > > > > Oops. Spoke too soon. While it doesn''t throw a syntax error. only the last > > ":scope => x" option is acted upon. So the question remains... > > > > Is there an easy way to accomplish nested scoping? > > > > Thanks much for any advice. > > > > Best regards, > > > > Bill > > > > > > ----- Original Message ----- > > From: Bill Walton > > To: rails@lists.rubyonrails.org > > > > Sent: 2006-03-26 1:21 PM > > Subject: Re: [Rails] validate_uniqueness_of {combination of fields} ??? > > > > > > I''ll be damned. The nested scoping works just the way I wrote it below. > > I''m posting this just so another newbie might be spared the angst. RoR > > ROCKS! > > > > ----- Original Message ----- > > From: Bill Walton > > To: rails@lists.rubyonrails.org > > Sent: 2006-03-26 12:47 PM > > Subject: Re: [Rails] validate_uniqueness_of {combination of fields} ??? > > > > > > I''ve realized in rereading my post below that I could have stated the > > problem I''m having more clearly. The system is multi-user, so the record > > set within which I''m trying to validate_uniqueness_of is constrained by a > > third field (e.g., ''group_id''). So I''m already using the "scope" option on > > validates_uniqueness_of. I think what I''m looking for is nested scoping. > > Something like: > > > > validates_uniqueness_of :fname, :scope => "lname", :scope => "group_id" > > > > Is there an easy (i.e., Rails) way to accomplish this? > > > > Thanks, > > Bill > > > > > > > > ----- Original Message ----- > > From: Bill Walton > > To: rails@lists.rubyonrails.org > > Sent: 2006-03-26 11:08 AM > > Subject: [Rails] validate_uniqueness_of {combination of fields} ??? > > > > > > > > I''ve looked at the validates documentation and it appears that > > validates_uniqueness_of works on only one field at a time. So I can, for > > example, validate that "sam" is unique to the record set, and that "smith" > > is unique to the record set. > > > > My problem is that each record includes two fields, the _combination_ of > > which must be unique to the record set, but each of which may not be unique. > > So "sam smith", "mary smith" and "sam jones" (stored as fname, lname) all > > need to pass the validation. > > > > > > Is there an easy way to validate_uniqueness_of a combination of fields > > within a record? > > > > Thanks in advance for any help. > > > > Best regards, > > Bill > > > > > > > > ________________________________ > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > ________________________________ > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > ________________________________ > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > -- > Dan Webb > http://www.danwebb.net >-- Dan Webb http://www.danwebb.net
Bill Walton
2006-Mar-27 14:17 UTC
[Rails] validate_uniqueness_of {combination of fields} ???
Hi Dan, Thanks much for the assist, but it doesn''t work. That is, the validate_uniqueness_of fails when AR can''t find a column named "full_name". If I''ve missed some nuance of your recommendation (wouldn''t surprise me to get a ''hey newbie, of course you have to do X in addition to what I sent you''), please set me straight. At this point, I guess I''m stuck with having to go the ugly route. Thanks much for your response, though. Best regards, Bill ----- Original Message ----- From: "Dan Webb" <dan@danwebb.net> To: <rails@lists.rubyonrails.org> Sent: 2006-03-26 4:46 PM Subject: Re: [Rails] validate_uniqueness_of {combination of fields} ??? oops, theres typos in that code. I should go to bed. Here it is again: def full_name [fname, lname].join '' '' end validates_uniqueness_of :full_name, :scope => "group_id" On 3/26/06, Dan Webb <dan@danwebb.net> wrote:> Hi Bill, > > I think you can achieve what you are after by defining a method like so: > > def fullname > [fname, lname]. join '' '' > end > > then validate that: > > validates_uniqueness_of :full_name, :scope => "group_id" > > That should do it. > > Cheers, > > Dan > > On 3/26/06, Bill Walton <bill.walton@charter.net> wrote: > > > > Oops. Spoke too soon. While it doesn''t throw a syntax error. only thelast> > ":scope => x" option is acted upon. So the question remains... > > > > Is there an easy way to accomplish nested scoping? > > > > Thanks much for any advice. > > > > Best regards, > > > > Bill > > > > > > ----- Original Message ----- > > From: Bill Walton > > To: rails@lists.rubyonrails.org > > > > Sent: 2006-03-26 1:21 PM > > Subject: Re: [Rails] validate_uniqueness_of {combination of fields} ??? > > > > > > I''ll be damned. The nested scoping works just the way I wrote it below. > > I''m posting this just so another newbie might be spared the angst. RoR > > ROCKS! > > > > ----- Original Message ----- > > From: Bill Walton > > To: rails@lists.rubyonrails.org > > Sent: 2006-03-26 12:47 PM > > Subject: Re: [Rails] validate_uniqueness_of {combination of fields} ??? > > > > > > I''ve realized in rereading my post below that I could have stated the > > problem I''m having more clearly. The system is multi-user, so therecord> > set within which I''m trying to validate_uniqueness_of is constrained bya> > third field (e.g., ''group_id''). So I''m already using the "scope" optionon> > validates_uniqueness_of. I think what I''m looking for is nestedscoping.> > Something like: > > > > validates_uniqueness_of :fname, :scope => "lname", :scope => "group_id" > > > > Is there an easy (i.e., Rails) way to accomplish this? > > > > Thanks, > > Bill > > > > > > > > ----- Original Message ----- > > From: Bill Walton > > To: rails@lists.rubyonrails.org > > Sent: 2006-03-26 11:08 AM > > Subject: [Rails] validate_uniqueness_of {combination of fields} ??? > > > > > > > > I''ve looked at the validates documentation and it appears that > > validates_uniqueness_of works on only one field at a time. So I can,for> > example, validate that "sam" is unique to the record set, and that"smith"> > is unique to the record set. > > > > My problem is that each record includes two fields, the _combination_ of > > which must be unique to the record set, but each of which may not beunique.> > So "sam smith", "mary smith" and "sam jones" (stored as fname, lname)all> > need to pass the validation. > > > > > > Is there an easy way to validate_uniqueness_of a combination of fields > > within a record? > > > > Thanks in advance for any help. > > > > Best regards, > > Bill > > > > > > > > ________________________________ > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > ________________________________ > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > ________________________________ > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > -- > Dan Webb > http://www.danwebb.net >-- Dan Webb http://www.danwebb.net _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Sorry, yes, that isn''t going to work is it. I''ll think on it more. Shouldn''t try to answer questions at 3 in the morning again. On 3/27/06, Bill Walton <bill.walton@charter.net> wrote:> Hi Dan, > > Thanks much for the assist, but it doesn''t work. That is, the > validate_uniqueness_of fails when AR can''t find a column named "full_name". > If I''ve missed some nuance of your recommendation (wouldn''t surprise me to > get a ''hey newbie, of course you have to do X in addition to what I sent > you''), please set me straight. At this point, I guess I''m stuck with having > to go the ugly route. > > Thanks much for your response, though. > > Best regards, > Bill > > > ----- Original Message ----- > From: "Dan Webb" <dan@danwebb.net> > To: <rails@lists.rubyonrails.org> > Sent: 2006-03-26 4:46 PM > Subject: Re: [Rails] validate_uniqueness_of {combination of fields} ??? > > > oops, theres typos in that code. I should go to bed. Here it is again: > > def full_name > [fname, lname].join '' '' > end > > validates_uniqueness_of :full_name, :scope => "group_id" > > > On 3/26/06, Dan Webb <dan@danwebb.net> wrote: > > Hi Bill, > > > > I think you can achieve what you are after by defining a method like so: > > > > def fullname > > [fname, lname]. join '' '' > > end > > > > then validate that: > > > > validates_uniqueness_of :full_name, :scope => "group_id" > > > > That should do it. > > > > Cheers, > > > > Dan > > > > On 3/26/06, Bill Walton <bill.walton@charter.net> wrote: > > > > > > Oops. Spoke too soon. While it doesn''t throw a syntax error. only the > last > > > ":scope => x" option is acted upon. So the question remains... > > > > > > Is there an easy way to accomplish nested scoping? > > > > > > Thanks much for any advice. > > > > > > Best regards, > > > > > > Bill > > > > > > > > > ----- Original Message ----- > > > From: Bill Walton > > > To: rails@lists.rubyonrails.org > > > > > > Sent: 2006-03-26 1:21 PM > > > Subject: Re: [Rails] validate_uniqueness_of {combination of fields} ??? > > > > > > > > > I''ll be damned. The nested scoping works just the way I wrote it below. > > > I''m posting this just so another newbie might be spared the angst. RoR > > > ROCKS! > > > > > > ----- Original Message ----- > > > From: Bill Walton > > > To: rails@lists.rubyonrails.org > > > Sent: 2006-03-26 12:47 PM > > > Subject: Re: [Rails] validate_uniqueness_of {combination of fields} ??? > > > > > > > > > I''ve realized in rereading my post below that I could have stated the > > > problem I''m having more clearly. The system is multi-user, so the > record > > > set within which I''m trying to validate_uniqueness_of is constrained by > a > > > third field (e.g., ''group_id''). So I''m already using the "scope" option > on > > > validates_uniqueness_of. I think what I''m looking for is nested > scoping. > > > Something like: > > > > > > validates_uniqueness_of :fname, :scope => "lname", :scope => "group_id" > > > > > > Is there an easy (i.e., Rails) way to accomplish this? > > > > > > Thanks, > > > Bill > > > > > > > > > > > > ----- Original Message ----- > > > From: Bill Walton > > > To: rails@lists.rubyonrails.org > > > Sent: 2006-03-26 11:08 AM > > > Subject: [Rails] validate_uniqueness_of {combination of fields} ??? > > > > > > > > > > > > I''ve looked at the validates documentation and it appears that > > > validates_uniqueness_of works on only one field at a time. So I can, > for > > > example, validate that "sam" is unique to the record set, and that > "smith" > > > is unique to the record set. > > > > > > My problem is that each record includes two fields, the _combination_ of > > > which must be unique to the record set, but each of which may not be > unique. > > > So "sam smith", "mary smith" and "sam jones" (stored as fname, lname) > all > > > need to pass the validation. > > > > > > > > > Is there an easy way to validate_uniqueness_of a combination of fields > > > within a record? > > > > > > Thanks in advance for any help. > > > > > > Best regards, > > > Bill > > > > > > > > > > > > ________________________________ > > > > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > > ________________________________ > > > > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > > ________________________________ > > > > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > > -- > > Dan Webb > > http://www.danwebb.net > > > > > -- > Dan Webb > http://www.danwebb.net > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Dan Webb http://www.danwebb.net
Jeff Coleman
2006-Mar-28 13:03 UTC
[Rails] Re: validate_uniqueness_of {combination of fields} ???
Looks like Rails 1.1 has the answer. From http://scottraymond.net/articles/2006/02/28/rails-1.1: "Allow validate_uniqueness_of to be scoped by multiple columns. See http://dev.rubyonrails.org/ticket/1559" Jeff -- Posted via http://www.ruby-forum.com/.
Jeff Coleman
2006-Mar-28 13:06 UTC
[Rails] Re: validate_uniqueness_of {combination of fields} ???
Jeff Coleman should have written:> > Looks like Rails 1.1 has the answer. From > http://scottraymond.net/articles/2006/02/28/rails-1.1 : > > "Allow validate_uniqueness_of to be scoped by multiple columns. See > http://dev.rubyonrails.org/ticket/1559 " > > Jeff-- Posted via http://www.ruby-forum.com/.