Searching for an existant entry in the Model

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!

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

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

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

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