Prev: Getting rid of self
Next: Fashion Accessories
From: Greg Ma on 16 Jun 2010 07:11 Hi, I have an array of events comming from a database and I have a new object (so without an id). I would like to check if my new object already exists in the database. I tried exists? and include? but obviously id doesn't work because my new object has no id. Is there a simple way to compare my object or I will have to compare each attribute? Greg -- Posted via http://www.ruby-forum.com/.
From: Brian Candler on 16 Jun 2010 08:16 Greg Ma wrote: > I have an array of events comming from a database and I have a new > object (so without an id). All objects have an object_id, so I presume you mean an "id" property/column. If you were using ActiveRecord, that's what you'd get by default. > Is there a simple way to compare my object or I will have to compare > each attribute? You'll have to look for another row in the database which matches on all the columns which matter to you. For example: you might consider that a 'User' already exists if there is an existing row with the same E-mail address - but you don't care if it has a different password or different last_login_time or whatever. In that case: u = User.new(...) if User.find_by_email(u.email) raise "That email address is already taken" end There is a race condition here, in that after testing for the non-existence of this user, another process could create it. So the safest way to deal with this is to have a unique constraint in the database itself. -- Posted via http://www.ruby-forum.com/.
From: Robert Klemme on 17 Jun 2010 02:19 On 16.06.2010 13:11, Greg Ma wrote: > I have an array of events comming from a database and I have a new > object (so without an id). > I would like to check if my new object already exists in the database. > I tried exists? and include? but obviously id doesn't work because my > new object has no id. > Is there a simple way to compare my object or I will have to compare > each attribute? Basically it boils down to that - there is no other way. How you do that is less clear, there are several options. You can do the check in memory if you have to load existing objects anyway. But you should also have proper constraints in the database which ensure you do not have duplicate instances. These should be used to avoid inserting a duplicate. You then only need to catch the error. Which brings us to the topic of primary keys: there is a lot debate about whether a numeric id is better or a composite PK which comprises key properties of your entity (class). There are arguments in favor of one and the other but even if you make the PK a surrogate key you should have a constraint which prevents duplicates according to your real PK. In a RDBMS uniqueness constraints are usually checked efficiently via an index which you can use to quickly check for the existing of a particular instance in the db. But see Brian's remark about race conditions. IMHO directly inserting and catching the error is the superior approach. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
|
Pages: 1 Prev: Getting rid of self Next: Fashion Accessories |