# Undefined method when creating a has\_many relationship

**URL:** <https://www.ruby-forum.com/t/undefined-method-when-creating-a-has-many-relationship/185149>\
**Category:** Rails\
**Created:** [March 15, 2010, 6:44am UTC](https://www.ruby-forum.com/t/undefined-method-when-creating-a-has-many-relationship/185149 "2010-03-15T06:44:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![markb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a8b319/32.png) [@markb](https://www.ruby-forum.com/u/markb)\
**Post date:** [March 15, 2010, 6:44am UTC](https://www.ruby-forum.com/t/undefined-method-when-creating-a-has-many-relationship/185149/1 "2010-03-15T06:44:08Z")

</div>

My first attempt at working with a new has\_many relationship is  
throwing an error in the view when I try to create a new ‘rcost’  
instance linked to a ‘resource’ (each resource can have multiple  
costs):

“undefined method `rcosts\_path’ for #ActionView::Base:0x715bc60”

Here is the view (new.html.erb) code that appears to be causing the  
error (I do not reference rcosts\_path anywhere in the view):

\<% form\_for(@rcost) do |f| %\>

Here are my model declarations:

class Resource \< ActiveRecord::Base  
has\_many :rcosts  
end

class Rcost \< ActiveRecord::Base  
belongs\_to :resource  
end

Here is my route declaration:

map.resources :resources, :has\_many =\> :rcosts

I first tried the build method in my rcosts\_controller:

@rcost = @resource.rcosts.build

Then tried new and manually setting the foreign key:

@rcost = @resource.rcosts.new  
@rcost.resource\_id = @resource.id

But neither worked.

I’d really appreciate it if someone could tell me what is going wrong  
and how I can create a new rcost instance associated with an existing  
resource instance.

Thanks in advance!  
Mark

---

<div class="post-metadata">

**Author:** ![markb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a8b319/32.png) [@markb](https://www.ruby-forum.com/u/markb)\
**Post date:** [March 15, 2010, 11:00am UTC](https://www.ruby-forum.com/t/undefined-method-when-creating-a-has-many-relationship/185149/2 "2010-03-15T11:00:28Z")

</div>

On Mar 15, 5:42 am, MarkB [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<% form\_for(@rcost) do |f| %\>

rcosts\_path is being invoked by form\_for, when it computes where the  
form should be posting too. Since you’ve got a nested resource you’ve  
got to tell form\_for which Resource this @rcost belongs to.

Fred

---

<div class="post-metadata">

**Author:** ![markb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a8b319/32.png) [@markb](https://www.ruby-forum.com/u/markb)\
**Post date:** [March 15, 2010, 7:27pm UTC](https://www.ruby-forum.com/t/undefined-method-when-creating-a-has-many-relationship/185149/3 "2010-03-15T19:27:28Z")

</div>

Thanks, Fred - very helpful orientation. I assumed (mistakenly,  
obviously) that form\_for would see the foreign key in @rcost and  
therefore know the @resource instance it should be associated with.

I searched around and found two recommendations that appear to address  
this issue I am having:

\<% form\_for([@resource, @rcost]) do |f| %\>  
and  
\<% form\_for([:resource, @rcost]) do |f| %\>

Not sure how behavior will differ with each version, but I will try  
them and report back on my results.

Mark

On Mar 15, 2:58 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![markb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a8b319/32.png) [@markb](https://www.ruby-forum.com/u/markb)\
**Post date:** [March 16, 2010, 6:01am UTC](https://www.ruby-forum.com/t/undefined-method-when-creating-a-has-many-relationship/185149/4 "2010-03-16T06:01:17Z")

</div>

Hi Fred,

That did it: \<% form\_for([@resource, @rcost]) do |f| %\>

It’s all obvious in hindsight.

Thanks again,  
Mark

On Mar 15, 2:58 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
