# Searching for an existant entry in the Model

**URL:** <https://www.ruby-forum.com/t/searching-for-an-existant-entry-in-the-model/143403>\
**Category:** Rails\
**Created:** [July 18, 2008, 3:43am UTC](https://www.ruby-forum.com/t/searching-for-an-existant-entry-in-the-model/143403 "2008-07-18T03:43:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 18, 2008, 3:43am UTC](https://www.ruby-forum.com/t/searching-for-an-existant-entry-in-the-model/143403/1 "2008-07-18T03:43:38Z")

</div>

Hi, I have a form for two models, a Requirement has\_many Posts

```
@requirement = Requirement.new(params[:requirement])
@post = @requirement.posts.build(params[:post])

```

That’s what I have so far to save the requirement. I can save the  
requirement fine. However, if the user inputs a title for a requirement  
that already exists, I want to check that, and add the user’s post to  
THAT requirement (that already exists). How would I do this? Requirement  
has only one column ‘title’…

I’ve tried

@requirements = Requirement.find(:all)

@requirements.each do |req|  
if(params[:requirement].include?(req.title))  
curr\_req = req  
end  
end

if curr\_req  
@post = curr\_req.posts.build(params[:post])  
if @post.save  
redirect\_to :action =\> :index  
else  
redirect\_to :action =\> :new\_requirement  
end  
else  
@requirement = Requirement.new(params[:requirement])  
@post = @requirement.posts.build(params[:post])

```
if @post.save
  redirect_to :action => :index
else
  redirect_to :action => :new_requirement
end

```

end

Doesn’t work… I also received an error: Unknown key(s): title

Any help is much appreciated, thanks!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 18, 2008, 8:35am UTC](https://www.ruby-forum.com/t/searching-for-an-existant-entry-in-the-model/143403/2 "2008-07-18T08:35:22Z")

</div>

On Jul 18, 2:43 am, Justin To [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’ve tried
> 
> @requirements = Requirement.find(:all)
> 
> @requirements.each do |req|  
> if(params[:requirement].include?(req.title))  
> curr\_req = req  
> end  
> end

That would work if you had instead said params[:requirement][:title]  
== req.title (not sure what you were thinking would happen with  
include?)  
This is an incredibly inefficient way of doing things when you could  
just do curr\_req = Requirement.find\_by\_title params[:requirement]  
[:title].

Fred

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 18, 2008, 6:10pm UTC](https://www.ruby-forum.com/t/searching-for-an-existant-entry-in-the-model/143403/3 "2008-07-18T18:10:01Z")

</div>

> That would work if you had instead said params[:requirement][:title]  
> == req.title (not sure what you were thinking would happen with  
> include?)  
> This is an incredibly inefficient way of doing things when you could  
> just do curr\_req = Requirement.find\_by\_title params[:requirement]  
> [:title].
> 
> Fred

Thanks for your continued help Fred, really appreciate it.

Pretty new to this stuff, so I’m still trying to learn.

Justin

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 18, 2008, 9:46pm UTC](https://www.ruby-forum.com/t/searching-for-an-existant-entry-in-the-model/143403/4 "2008-07-18T21:46:35Z")

</div>

Justin To wrote:

> > That would work if you had instead said params[:requirement][:title]  
> > == req.title (not sure what you were thinking would happen with  
> > include?)  
> > This is an incredibly inefficient way of doing things when you could  
> > just do curr\_req = Requirement.find\_by\_title params[:requirement]  
> > [:title].
> > 
> > Fred
> 
> Thanks for your continued help Fred, really appreciate it.
> 
> Pretty new to this stuff, so I’m still trying to learn.
> 
> Justin

why not

class requirement

validates\_uniqueness\_of :title

end
