Duplicate entry - how to check if an id exist before saving?

How do I check if an entry exists before saving?

Someone one told me to use the method find_or_create (or something like
that)
but it didn’t work because I think the version of rails that we have is
not the most recent.

I need a way to check if an id exists in the db before saving. Any
suggestions?

Thank you

cranberry wrote:

How do I check if an entry exists before saving?

Someone one told me to use the method find_or_create (or something like
that)
but it didn’t work because I think the version of rails that we have is
not the most recent.

I need a way to check if an id exists in the db before saving. Any
suggestions?

Thank you

exists?(id) (like: Person.exists?(5))


Agnieszka F.

Why do you need to know this? Are you afraid of creating duplicate
records? Don’t be, as rails knows whether the instance of the model
you have is a new row or not. If it is new, then saying
my_model_instance.save will insert a record, otherwise it will
perform an update.

Well, hey, if Rails know, then I wanna know too! Ok, then try:

my_model_instance.new_record?

If the record is new, then you can be sure that the id doesn’t
exist. If it returns nil, then you can bet that the id is in the
table already.

-Derrick S.

How would you handle a situation where there was a second field that
needed to be unique? I wouldn’t want that second field to fail and then
send a message back to the user. I would want that second field to be
incremented if needed and then save. How would I do this? Sorry to
jump in on this thread, but it sounded very related.

Charlie
recentrambles.com

I would think this would be database-specific. Most databases will
allow you to define a column other than the primary key to be unique
and to auto-increment. For instance, in OpenBase we have a built-in
field called _rowid which is guaranteed to be unique for every row.
Therefore, I would declare the column I wanted like so:

my_other_key INT UNIQUE DEFAULT _rowid

However, I am curious in what situation you would find yourself with
a unique column that wasn’t the primary key, but that you could
increment without input from the user? In my experience, unique
columns other than the primary key usually have something to do with
login, account number, or something else that requires input from the
user. I’m not trying to put down your design, I’m just genuinely
curious.

-Derrick S.

you can add a validation to your model if you are paranoid, but i dont
think
you need to worry about this if its an auto_increment primary key type
of
field.

validates_uniqeness_of :id

adam

Currently we base our primary key around a timestamp. This sometimes
fails when multiple users log on simultaneously. We currently check to
see if someone already has that timestamp and if they do we increment
the number by one (ie 20060120152905 would become 20060120152906). I’m
curious how this situation would be handled in Rails. It’s very simple
in perl, but how would rails work since you have your validation is in
your model and all errors are sent back to the user.

charlie bowman
recentrambles.com