Uniqueness validation perplexity

I want to write a validate routine to check to enforce that a position
must be unique in a category. (In another category, it doesn’t have
to and shouldn’t need to be unique.) I write this code which works
happily for new items:

def position_in_category_not_unique
@items = Item.find( :all, :conditions => [ “category_id = ? AND
position = ?”, category_id, position ] )
if @items.size > 0
errors.add_to_base…

If, however, I try to update an existing item, because it’s already
in the database, it gives the error.

Any suggestions on how to approach this? It’s a conceptual question.
Do I handle this through the controller somehow?

Many, many, many TIA if you tackle this one :slight_smile:
Craig

On Fri, Sep 25, 2009 at 4:14 PM, Dudebot [email protected] wrote:

If, however, I try to update an existing item, because it’s already
in the database, it gives the error.

What error are you seeing? What code are you running to update the item?

Any suggestions on how to approach this? It’s a conceptual question.
Do I handle this through the controller somehow?

If it’s a validation check, it should be in the model.

Colin

On Sep 26, 12:14 am, Dudebot [email protected] wrote:

If, however, I try to update an existing item, because it’s already
in the database, it gives the error.

Any suggestions on how to approach this? It’s a conceptual question.
Do I handle this through the controller somehow?

You could add a condition along the lines of AND id != self.id. Or
you could use validates_uniqueness of, which already handles this.

Fred

Many thanks, Colin & Frederick–I ended up with this:

def position_in_category_not_unique
@items = Item.find( :all, :conditions => [ “category_id = ? AND
position = ?”, category_id, position ] )
single = Item.first( :all, :conditions => [ “category_id = ? AND
position = ?”, category_id, position ] )
if @items.size > 1 || ( single && single.id != id )
errors.add_to_base “Position #{ position } already exists in #
{ Category.find( category_id ).header }.”
end
end

Which works, but seems ugly. Frederick–I’m not sure how to use
validates_uniqueness_of with more than one field–can that be done?

Again, many thanks,
Craig

On Sep 25, 9:35 pm, Frederick C. [email protected]

Dudebot wrote:
[…]

Frederick–I’m not sure how to use
validates_uniqueness_of with more than one field–can that be done?

I’m going to suggest that you spend less time posting here and more time
reading docs. 20 seconds of reading the documentation for
validates_uniqueness_of would have told you about the :scope option.
Look it up. :slight_smile:

Again, many thanks,
Craig

On Sep 25, 9:35�pm, Frederick C. [email protected]

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I appreciate the help, Marnen. In fact, I have
http://api.rubyonrails.org/
permanently up while coding rails, and it’s the tab to the left of my
email as I write this. I always go first to it, then the guides, then
a Google search, and only then do I post here. However, as one new to
rails, I don’t “get it” yet. Unlike ruby, rails is all about
nomenclature and formalisms. I’m still learning those, and it was
easy to overlook the “:scope” option when I re-read
“validates_uniqueness_of”'s documentation for the fourth time. You
may forget what it was like to first enter rails, but in a formalism
rich environment, human navigation can be priceless–as it was in your
post. So, thanks.
Craig

On Sep 26, 12:15 am, Marnen Laibow-Koser <rails-mailing-l…@andreas-

Dudebot wrote:

I appreciate the help, Marnen. In fact, I have
http://api.rubyonrails.org/
permanently up while coding rails, and it’s the tab to the left of my
email as I write this. I always go first to it, then the guides, then
a Google search, and only then do I post here. However, as one new to
rails, I don’t “get it” yet.

I understand. The hardest thing for me has often been finding features
I know must exist, but whose names I don’t know.

Unlike ruby, rails is all about
nomenclature and formalisms.

Not really, except in some limited parts of the framework.

I’m still learning those, and it was
easy to overlook the “:scope” option when I re-read
“validates_uniqueness_of”'s documentation for the fourth time.

If on the fourth reading, you are overlooking things, then perhaps your
docs reading skills are at issue here.

And I’m not trying to be snarky by saying that. I just want to remind
you that getting the most out of something like Rails is not only a
question of technical skills.

You
may forget what it was like to first enter rails,

I do not. I know that it can be tough.

but in a formalism
rich environment, human navigation can be priceless

Agreed. The only reason I responded with “RTFM” was that in this case,
you clearly knew the name of the function you needed to call, so you
would know where to look.

–as it was in your

post. So, thanks.

You’re welcome.

Craig

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Oh, I definitely admit that my “docs reading skills” are imperfect!
Maybe in my next life I’ll be granted the get-it-all-at-once-when-
reading gift, but not in this one :slight_smile: Anyway, you helped me cut out
unnecessary code and I learned quite a bit in the process, so I am
grateful.

On Sep 26, 8:22 am, Marnen Laibow-Koser <rails-mailing-l…@andreas-

On Saturday 26 September 2009, Dudebot wrote:

Oh, I definitely admit that my “docs reading skills” are imperfect!
Maybe in my next life I’ll be granted the get-it-all-at-once-when-
reading gift, but not in this one :slight_smile: Anyway, you helped me cut out
unnecessary code and I learned quite a bit in the process, so I am
grateful.

Have you ever considered reading the Rails source code? Not everything,
of course, but some of the bits and pieces that interest you and that
give you trouble. Spent as much or as little time as you like and
you’ll find it is well spent. Reading and understanding the source is
within the reach of every programmer seriously working with Rails.

There are about three places that are truly involved (in the 2.x
series): joins and associations in ActiveRecord, view templates, and
routing. So don’t start your reading at the deep end and you’ll be
fine.

I’m making this suggestion every once in a while and usually the
reaction is not particularly heartfelt. Anyway, don’t take this
personally, see it as an invitation, not an exhortation.

Michael


Michael S.
mailto:[email protected]
http://www.schuerig.de/michael/

Michael S. wrote:

On Saturday 26 September 2009, Dudebot wrote:

Oh, I definitely admit that my “docs reading skills” are imperfect!
Maybe in my next life I’ll be granted the get-it-all-at-once-when-
reading gift, but not in this one :slight_smile: Anyway, you helped me cut out
unnecessary code and I learned quite a bit in the process, so I am
grateful.

Have you ever considered reading the Rails source code? Not everything,
of course, but some of the bits and pieces that interest you and that
give you trouble. Spent as much or as little time as you like and
you’ll find it is well spent. Reading and understanding the source is
within the reach of every programmer seriously working with Rails.
[…]

Great advice. I really must find the time to do more of that.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Have you ever considered reading the Rails source code? Not everything,

Actually, I usually do–I come to rails after several years of coding
in ruby, so I find it interesting how things are done, and there’s
much to be learned from how rails uses ruby. I’m just having this
really hard time navigating the docs. Don’t get me wrong–I think the
docs are great, and I can’t imagine how such a feature rich
environment could be doc’d differently–it’s just very difficult for
me the newbie (not necessarily any newbie, probably just me) to figure
out how to use them well. I’m still waiting for that lightbulb moment
(like the one that happened with the C++ STL so long ago) where I know
where to look and how to use what I find. Oddly, I never needed it
with ruby itself–I just sort of used the docs naturally from day 1.

Case in point: last night I was trying to figure out how to implement
a drop-down list for fields from a single db. I’m using
collection_select elsewhere in the code to reference 1 db linked to
another, but I wanted to pull an id from a single db in a drop down
list. I finally gave up after several hours of reading the docs,
searching and trying different things. I could have built an array
from the db, then used another _for_select method I suppose, but I
just know a better way exists, and so I put it on the back burner as
I’ve recently been scolded for posting too much here. I know that
someday when I learn how to do it, I’ll hit myself on the side of my
head like I’ve done so often with rails and say, “I didn’t see
that”? But that great chasm between problem and solution seems
especially wide for rails for me. What makes it truly frustrating of
course is that rails solutions are so simple and beautiful, so I
always know a good one is lurking out there, just beyond my grasp.

Anyway, I’m likely just dense as a brick. Thanks, though, to everyone
who have patiently answered my questions with really nice, long
replies and sample code–they’ve been priceless. I couldn’t have
built this project (obviously my first rails one) without your help.

Craig

Dudebot wrote:

Have you ever considered reading the Rails source code? Not everything,

Actually, I usually do–I come to rails after several years of coding
in ruby, so I find it interesting how things are done, and there’s
much to be learned from how rails uses ruby. I’m just having this
really hard time navigating the docs.

If you’re using the ones at api.rubyonrails.org, then of course you’re
having trouble navigating them. Try http://www.railsbrain.com or
http://www.railsapi.com – same info, but more easily searchable.

Don’t get me wrong–I think the
docs are great, and I can’t imagine how such a feature rich
environment could be doc’d differently–it’s just very difficult for
me the newbie (not necessarily any newbie, probably just me) to figure
out how to use them well.

Read docs for whole classes, not just isolated methods. That will help.

Read class preambles. There’s a lot of info in there, including
dynamically faked methods that won’t show up in the method list (like AR
finders).

I’m still waiting for that lightbulb moment
(like the one that happened with the C++ STL so long ago) where I know
where to look and how to use what I find.

Look in the same class as a similar method you already used. Chances
are what you want will be there.

Oddly, I never needed it
with ruby itself–I just sort of used the docs naturally from day 1.

Ruby and Rails are more than just collections of methods. Read the
class preambles (Rails) and the pickaxe book (Ruby), and make sure you
understand the way the language and framework work. Also take note of
the :options that Rails likes to use.

Case in point: last night I was trying to figure out how to implement
a drop-down list for fields from a single db. I’m using
collection_select elsewhere in the code to reference 1 db linked to
another, but I wanted to pull an id from a single db in a drop down
list. I finally gave up after several hours of reading the docs,
searching and trying different things. I could have built an array
from the db, then used another _for_select method I suppose, but I
just know a better way exists, and so I put it on the back burner as
I’ve recently been scolded for posting too much here.

If you’re referring to my scolding, then I think you misunderstood me.
By all means, ask intelligent questions as much as you like! – once
you’ve read the docs carefully. If you’ve done that (as in this case)
and still not found the answer, please do post here.

[…]

Anyway, I’m likely just dense as a brick.

Well, you said it… :smiley:

Thanks, though, to everyone
who have patiently answered my questions with really nice, long
replies and sample code–they’ve been priceless. I couldn’t have
built this project (obviously my first rails one) without your help.

You’re most welcome.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Craig

If you’re using the ones at api.rubyonrails.org, then of course you’re
having trouble navigating them. Tryhttp://www.railsbrain.comorhttp://www.railsapi.com-- same info, but more easily searchable.

That’s very useful, many thanks. Bookmark x 2!

Read docs for whole classes, not just isolated methods. That will help.

I have! and the preambles, similar methods, etc. I always start at
the top, then look down :slight_smile:

Ruby and Rails are more than just collections of methods. Read the
class preambles (Rails) and the pickaxe book (Ruby), and make sure you

I learned Ruby from the pickaxe book when there were no other docs
around :slight_smile: Ruby is my go-to language now–I just maintained a bit of
non-Rails code this afternoon before turning back to this list,
discovering (OT, sorry) that Snow Leopard broke the Tk bindings.
Bleacch. But there is a difference between the structures that Ruby
allows you to do and what Rails needs you to do. In Ruby, you can do
something inelegant, but it works. That can be useful when you’re new
to something, as you can get your feet wet, and then as you understand
what kinds of pithy expressions can replace the inelegant ones and
why, you “grow into” the language. Rails seems to be like that to an
extent, but the learning curve is steep. That’s obviously a
consequence of one being a framework built on the other, but
nonetheless Rails seems harder to learn. It’s OK–I may be dense as a
brick, but I’m stubborn, too.

If you’re referring to my scolding,…

Actually as far as a RTFM post goes, it was extraordinarily
delicate :slight_smile: It was more of darned-if-I’m-going-to-look-like-an-idiot-
again-tonight last night, but today I’m apparently back to happily
looking like an idiot.

Thanks for the links!
Craig