From: Brian Candler Date: 2010-06-16T21:16:33+09:00 Subject: Re: Compare two objects 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/.