cranberry
2006-Feb-24 13:39 UTC
[Rails] 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 -- Posted via http://www.ruby-forum.com/.
Agnieszka Figiel
2006-Feb-24 14:11 UTC
[Rails] Re: Duplicate entry - how to check if an id exist before sav
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 youexists?(id) (like: Person.exists?(5)) -- Agnieszka Figiel -- Posted via http://www.ruby-forum.com/.
Derrick Spell
2006-Feb-24 14:18 UTC
[Rails] Duplicate entry - how to check if an id exist before saving?
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 Spell On Feb 24, 2006, at 8:39 AM, 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Adam Denenberg
2006-Feb-24 14:27 UTC
[Rails] Duplicate entry - how to check if an id exist before saving?
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 On 2/24/06, Derrick Spell <derrickspell@cdmplus.com> wrote:> > 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 Spell > > On Feb 24, 2006, at 8:39 AM, 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 > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > 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/20060224/b47133d3/attachment.html
Charlie Bowman
2006-Feb-24 14:51 UTC
[Rails] Duplicate entry - how to check if an id exist before saving?
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 On Fri, 2006-02-24 at 09:27 -0500, Adam Denenberg wrote:> 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 > > > On 2/24/06, Derrick Spell <derrickspell@cdmplus.com> wrote: > > 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 Spell > > On Feb 24, 2006, at 8:39 AM, 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 > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > 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/20060224/596e1978/attachment-0001.html
Derrick Spell
2006-Feb-24 15:26 UTC
[Rails] Duplicate entry - how to check if an id exist before saving?
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 Spell On Feb 24, 2006, at 9:51 AM, Charlie Bowman wrote:> 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 > > On Fri, 2006-02-24 at 09:27 -0500, Adam Denenberg wrote: >> 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-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060224/a5fa1eb2/attachment.html
Charlie Bowman
2006-Feb-24 15:39 UTC
[Rails] Duplicate entry - how to check if an id exist before saving?
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 On Fri, 2006-02-24 at 10:26 -0500, Derrick Spell wrote:> 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 Spell > > > On Feb 24, 2006, at 9:51 AM, Charlie Bowman wrote: > > > > > 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 > > > > On Fri, 2006-02-24 at 09:27 -0500, Adam Denenberg wrote: > > > > > 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 > > > > > _______________________________________________ > 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/20060224/04c92a90/attachment.html