I''m new to Rails and I''ve been playing around with it''s features. I was trying to validate the uniquess of multiple columns via the validates_uniqueness_of method of ActiveRecord. I tried the following: validates_uniqueness_of :title, :author_id, :message => "A book already exists with this title and author" Instead of validating the uniqueness of the combination of title and author_id (since a book is uniquely identified by the combination of its title and its author), it validated the uniqueness of the title and of the author separately. It would then not allow two books by the same author to be entered. Is there a way to validate the uniqueness of a concatenated key ? Find local movie times and trailers on Yahoo! Movies. http://au.movies.yahoo.com
For your problem this I believe it will suffice: validates_uniqueness_of :title, :scope => :author_id, :message => "A book already exists with this title and author" However if you would have more than two field combinations required for uniqueness, there would be no solution using validates_uniqueness_of. Reason is that only attribute plus another for the scope is accepted. Currently at least ... Regads, Zsombor On 5/5/05, Matthew Keene <dfg778-/E1597aS9LT0CCvOHzKKcA@public.gmane.org> wrote:> I''m new to Rails and I''ve been playing around with > it''s features. I was trying to validate the uniquess > of multiple columns via the validates_uniqueness_of > method of ActiveRecord. I tried the following: > > validates_uniqueness_of :title, :author_id, :message > => "A book already exists with this title and author" > > Instead of validating the uniqueness of the > combination of title and author_id (since a book is > uniquely identified by the combination of its title > and its author), it validated the uniqueness of the > title and of the author separately. It would then not > allow two books by the same author to be entered. > > Is there a way to validate the uniqueness of a > concatenated key ? > > Find local movie times and trailers on Yahoo! Movies. > http://au.movies.yahoo.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- http://deezsombor.blogspot.com
--- Matthew Keene <dfg778-/E1597aS9LT0CCvOHzKKcA@public.gmane.org> wrote:> I''m new to Rails and I''ve been playing around with > it''s features. I was trying to validate the > uniquess > of multiple columns via the validates_uniqueness_of > method of ActiveRecord.OK, I''m going to answer my own question here (finding the examples directory under the activerecord distribution was a wonderful thing). I added my own validation for the uniqueness of the combination and overrode the validate methods, as shown below. Is this the only way to do this ? I guess I''d be a little disappointed if this sort of thing wasn''t available within the framework - this seems like a very common real world case. class Book < ActiveRecord::Base belongs_to :author validates_presence_of :author validates_numericality_of :num_pages #validates_uniqueness_of :title, :author_id, # :message => "A book already exists with this title and author" def self.duplicate_book?(title,author_id,id=nil) logger.info("Checking for duplicate books - title " + title + " author = " + author_id.to_s) if id.nil? condition = ["title = ''%s'' AND author_id %d",title,author_id] else condition = ["title = ''%s'' AND author_id = %d and id <> %d",title,author_id,id] end !find_first(condition).nil? end def validate_on_create if Book.duplicate_book?(title,author_id) errors.add_to_base("Another book exists with the same author and title") end super end def validate_on_update if Book.duplicate_book?(title,author_id,id) errors.add_to_base("Another book exists with the same author and title") end super end end Find local movie times and trailers on Yahoo! Movies. http://au.movies.yahoo.com
Hello, I think that this is impossible with this method. You''ll have to overwrite validate method with some custom validation steps. If I get it right :-). On 5.5.2005, at 12:24, Matthew Keene wrote:> I''m new to Rails and I''ve been playing around with > it''s features. I was trying to validate the uniquess > of multiple columns via the validates_uniqueness_of > method of ActiveRecord. I tried the following: > > validates_uniqueness_of :title, :author_id, :message > => "A book already exists with this title and author" > > Instead of validating the uniqueness of the > combination of title and author_id (since a book is > uniquely identified by the combination of its title > and its author), it validated the uniqueness of the > title and of the author separately. It would then not > allow two books by the same author to be entered. > > Is there a way to validate the uniqueness of a > concatenated key ? > > Find local movie times and trailers on Yahoo! Movies. > http://au.movies.yahoo.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >Pepe
I''ve used this technique successfully. I had two validates_uniqueness_of calls to validate both fields. If this is so important, I''d suggest adding the functionality as a patch. On 5/5/05, Dee Zsombor <dee.zsombor-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> For your problem this I believe it will suffice: > > validates_uniqueness_of :title, :scope => :author_id, :message > => "A book already exists with this title and author" > > However if you would have more than two field combinations required > for uniqueness, there would be no solution using > validates_uniqueness_of. Reason is that only attribute plus another > for the scope is accepted. Currently at least ...-- rick http://techno-weenie.net
I''ve used it too, but with only one validation call. I reckon that if *field1* is unique in the scope of *field2* than *field2* is also unique in the scope of *field1*. In fact SQL select command generated by ActiveRecord has the where part looking like : "field1 = value1 AND field2 = value2" (plus some primary key conditions) Therefore a single validates_uniqueness_of call will suffice. Cheers, Zsombor On 5/5/05, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve used this technique successfully. I had two > validates_uniqueness_of calls to validate both fields. > > If this is so important, I''d suggest adding the functionality as a patch. > > On 5/5/05, Dee Zsombor <dee.zsombor-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > For your problem this I believe it will suffice: > > > > validates_uniqueness_of :title, :scope => :author_id, :message > > => "A book already exists with this title and author" > > > > However if you would have more than two field combinations required > > for uniqueness, there would be no solution using > > validates_uniqueness_of. Reason is that only attribute plus another > > for the scope is accepted. Currently at least ... > > -- > rick > http://techno-weenie.net >-- http://deezsombor.blogspot.com