On Tue, Mar 30, 2010 at 2:38 AM, Derek C.
[email protected]wrote:
etc. I just want to make sure the movie’s title and release date don’t
@movie_db = MovieDb.new(“title”=>i.title,“plot”=>i.plot,etc)
Hi, Derek, there is a ML specifically for Rails
http://groups.google.com/group/rubyonrails-talk
The short answer is that you’re doing way too much work (and what a nice
short answer that is 
You don’t need to worry about the DB as much as you are, let
ActiveRecord do
that for you. Your model can declare validations that need to be met,
such
as “this attribute should be unique”, and then AR will not save any data
to
the db if that validation does not pass.
In your case, the model might look like this
class Movie < ActiveRecord::Base
validates_uniqueness_of :title , :scope => :genre , :message =>
“title/genre pair must be unique”
end
Here is an example that shows a session, where I create several movies,
some
of which are duplicates. You see that it does not save the duplicates
because they fail this validation. Then I query the db for all the
movies it
contains, and you will see that there are no duplicate movies listed in
there.
http://img202.imageshack.us/img202/213/picture6os.png
You shouldn’t be replicating the db in memory, and you shouldn’t be
validating data outside your model like this (I assume this code is in
your
controller).
Anyway, hope that helps, and for an exceptionally good resource on
Rails,
check out guides.rubyonrails.org In this particular case, the
information to
create the validation was in the guide titled “Active Record Validations
and
Callbacks”. I know there is a lot there, but with all seriousness, the
time
you spend reading these, and getting familiar with what is available
(even
if it’s just to make a mental note of it, and then later look it up when
you
need it) will save you hundreds more time and effort (and lets not
forget
frustration) in the long run. Because Rails will do things for you that
just
make your life so much easier, and that you might spend several days
working
on to create a slower buggier solution to something there is already a
method for, or that AR already accounts for. I speak from experience on
that
