# Newbie question

**URL:** <https://www.ruby-forum.com/t/newbie-question/160288>\
**Category:** Rails\
**Created:** [February 13, 2009, 7:22pm UTC](https://www.ruby-forum.com/t/newbie-question/160288 "2009-02-13T19:22:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [February 13, 2009, 7:22pm UTC](https://www.ruby-forum.com/t/newbie-question/160288/1 "2009-02-13T19:22:12Z")

</div>

Hey all,

I’ve been working with rails for a grand total of 8 hours, and have run  
up against the first problem I haven’t been able to adequately solve  
using google.

So I’ve got a schema that basically consists of a bunch of researchers.  
Each researcher has only one notebook. I’m trying to ensure that only  
one notebook can be created for each user. Below is the code I’ve been  
using in the create method for my notebook:

existingbook = Notebook.find(:first, params[:researcher\_id])  
@notebook = Notebook.new(params[:notebook])

```
respond_to do |format|
  if (existingbook.nil?)
    if @notebook.save
      flash[:notice] = 'Notebook was successfully created.'
      format.html { redirect_to(@notebook) }
      format.xml { render :xml => @notebook, :status => :created,

```

:location =\> @notebook }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @notebook.errors, :status =\>  
:unprocessable\_entity }  
end  
else  
flash[:error] = ‘A notebook already exists for this user’  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @notebook.errors, :status =\>  
:unprocessable\_entity }  
end  
end

The basic idea is to check the notebook table for the researcher id  
specified by the user. If a notebook already exists, send an error  
message back. Otherwise, create the notebook as usual. However, the  
above code doesn’t work like I think it should. Basically what I have  
above always allows another notebook to be created (e.g. I can enter  
researcher\_id of 1 five times in a row without an error). Is there  
something I’m missing? Is there any way to check the database to see if  
a value exists already before saving it off? I’m probably missing  
something simple here, and if anyone has some input I would greatly  
appreciate the assistance.

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [February 13, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/newbie-question/160288/2 "2009-02-13T20:07:24Z")

</div>

On 13 Feb 2009, at 18:22, Matt Billock wrote:

> Each researcher has only one notebook. I’m trying to ensure that only  
> one notebook can be created for each user. Below is the code I’ve been  
> using in the create method for my notebook:
> 
> existingbook = Notebook.find(:first, params[:researcher\_id])

This doesn’t find a notebook with the specified researcher\_id. It just  
finds a notebook (and thinking about it, how could Notebook.find know  
that the parameter you were giving it was a researched\_id and not  
something else?\_

If you check log/development.log while you’re doing this you can see  
the sql queries generated which would reveal this.

What you probably meant to write is  
Notebook.find\_by\_researcher\_id(params[:researcher\_id]) or if you  
wanted to be long winded

Notebook.find :first, :conditions =\> [“researcher\_id = ?”,  
params[:researcher\_id]]

Lastly, take a look at validates\_uniqueness\_of and be aware of the  
race condition inherent in the way it works (your code suffers from  
the same problem)

Fred

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [February 13, 2009, 8:30pm UTC](https://www.ruby-forum.com/t/newbie-question/160288/3 "2009-02-13T20:30:41Z")

</div>

Frederick C. wrote:

> On 13 Feb 2009, at 18:22, Matt Billock wrote:
> 
> This doesn’t find a notebook with the specified researcher\_id. It just  
> finds a notebook (and thinking about it, how could Notebook.find know  
> that the parameter you were giving it was a researched\_id and not  
> something else?\_
> 
> If you check log/development.log while you’re doing this you can see  
> the sql queries generated which would reveal this.
> 
> What you probably meant to write is  
> Notebook.find\_by\_researcher\_id(params[:researcher\_id]) or if you  
> wanted to be long winded
> 
> Notebook.find :first, :conditions =\> [“researcher\_id = ?”,  
> params[:researcher\_id]]
> 
> Lastly, take a look at validates\_uniqueness\_of and be aware of the  
> race condition inherent in the way it works (your code suffers from  
> the same problem)
> 
> Fred

Thanks much for the reply! I figured it was something simple like that.  
I just implemented the validates\_uniqueness\_of attribute before coming  
back here and it does the job much better than I could 🙂

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [February 14, 2009, 6:25am UTC](https://www.ruby-forum.com/t/newbie-question/160288/4 "2009-02-14T06:25:38Z")

</div>

Hey man. Welcome! First thing, from hereonin, please put more  
meaningful subject names. We get lots of noob questions. Second, what  
you’d like is belongs\_to association. Take a look in  
activerecord::base documentation at [api.rubyonrails.org](http://api.rubyonrails.org)

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

On 14/02/2009, at 5:22 AM, Matt Billock  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [February 14, 2009, 11:13am UTC](https://www.ruby-forum.com/t/newbie-question/160288/5 "2009-02-14T11:13:32Z")

</div>

Create a unique index for the foreign key (user\_id) in the notebook  
table!
