# Relationships not working

**URL:** <https://www.ruby-forum.com/t/relationships-not-working/173465>\
**Category:** Rails\
**Created:** [August 9, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/relationships-not-working/173465 "2009-08-09T23:40:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sweetie\_lolz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@sweetie\_lolz3](https://www.ruby-forum.com/u/sweetie_lolz3)\
**Post date:** [August 9, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/relationships-not-working/173465/1 "2009-08-09T23:40:09Z")

</div>

I’ve just stated programming with Ruby on Rails a few days ago. I’m  
trying to set up a simple Blog. To this point I have a Posts.rb model  
and controller and a comments.rb model and controller with a “has\_many  
:comments” in posts.rb and a “belongs\_to :posts” in comments.rb.  
Furthermore, the comments table has a post\_id column of type integer (Am  
I right in that this is supposed to be automatically assigned the id of  
the post p when p.comment.new() is called?).

However, in the ruby console after assigning a post object to p (\>\> p =  
Posts.find(1)) the command

> > p.comment.new(:author =\> ‘me’, :content =\> ‘Enter all the content you desire here…’)

returns the error:  
NoMethodError: undefined method `comment' for #<Posts:0x19673f4> from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/attribute_methods.rb:260:in`method\_missing’  
from (irb):4

I get the same error when I use p.comment.create and p.comment.build  
When I execute

> > p.comments.new(:author =\> ‘me’, :content =\> ‘Enter all the content you desire here…’)

I get the error:  
NameError: uninitialized constant Posts::Comment  
from  
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active\_support/dependencies.rb:105:in  
`const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2199:in`compute\_type’  
from  
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active\_support/core\_ext/kernel/reporting.rb:11:in  
`silence_warnings' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2195:in`compute\_type’  
from  
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active\_record/reflection.rb:156:in  
`send' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/reflection.rb:156:in`klass’  
from  
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active\_record/reflection.rb:187:in  
`quoted_table_name' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/has_many_association.rb:96:in`construct\_sql’  
from  
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active\_record/associations/association\_collection.rb:21:in  
`initialize' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations.rb:1265:in`new’  
from  
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active\_record/associations.rb:1265:in  
`comments’  
from (irb):7

What am I doing wrong? Is there another method I’m supposed to be  
coding? Are there more steps to creating a relationship other than 1)add  
has\_many to the parent, 2) add belongs\_to to the child and 3)ensure  
childl has a parent\_id column?

Thanks you for any help you an give me! I’ve been trying to look on  
forumns but haven’t yet found someone with the same problem…

* * *

## CODE:

posts.rb  
class Posts \< ActiveRecord::Base  
validates\_presence\_of :title, :byline, :content, :author  
has\_many :comments  
end

posts\_controller.rb  
class PostsController \< ApplicationController  
def index

```
end
def new
    @post = Posts.new(params[:posts])
    if request.post? and @post.save
        flash[:notice] = 'Post submission succeeded'
        redirect_to :action => 'index'
    end
end

```

end

comments.rb  
class Comments \< ActiveRecord::Base  
belongs\_to :posts  
end

comments\_controller.rb  
class CommentsController \< ApplicationController  
def index

```
end
def new
    @comment = Comments.new(params[:comments])
    if request.post? and @comment.save
        flash[:notice] = 'Comment submission succeeded'
    end
end

```

end

---

<div class="post-metadata">

**Author:** ![sweetie\_lolz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@sweetie\_lolz3](https://www.ruby-forum.com/u/sweetie_lolz3)\
**Post date:** [August 9, 2009, 11:50pm UTC](https://www.ruby-forum.com/t/relationships-not-working/173465/2 "2009-08-09T23:50:03Z")

</div>

On Aug 9, 10:40 pm, Audrina S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> However, in the ruby console after assigning a post object to p (\>\> p =  
> Posts.find(1)) the command
> 
> > > p.comment.new(:author =\> ‘me’, :content =\> ‘Enter all the content you desire here…’)

You association is called comments so you need to write p.comments.  
The method is called build (not new) so p.comments.build(…). You  
should also watch your singulars/plurals: class names should be  
singular or weird stuff may happen further down the line.

Fred

---

<div class="post-metadata">

**Author:** ![sweetie\_lolz3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@sweetie\_lolz3](https://www.ruby-forum.com/u/sweetie_lolz3)\
**Post date:** [August 10, 2009, 12:06am UTC](https://www.ruby-forum.com/t/relationships-not-working/173465/3 "2009-08-10T00:06:51Z")

</div>

Frederick C. wrote:

> You association is called comments so you need to write p.comments.  
> The method is called build (not new) so p.comments.build(…). You  
> should also watch your singulars/plurals: class names should be  
> singular or weird stuff may happen further down the line.

I tried the following but still got errors:

> > p.comments.build(:author =\> ‘me’, :content =\> ‘Whatever content…’)  
> > NameError: uninitialized constant Posts::Comment  
> > from  
> > /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active\_support/dependencies.rb:105:in  
> > `const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2199:in`compute\_type’  
> > from  
> > /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active\_support/core\_ext/kernel/reporting.rb:11:in  
> > `silence_warnings' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2195:in`compute\_type’  
> > from  
> > /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active\_record/reflection.rb:156:in  
> > `send' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/reflection.rb:156:in`klass’  
> > from  
> > /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active\_record/reflection.rb:187:in  
> > `quoted_table_name' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/has_many_association.rb:96:in`construct\_sql’  
> > from  
> > /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active\_record/associations/association\_collection.rb:21:in  
> > `initialize' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations.rb:1265:in`new’  
> > from  
> > /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active\_record/associations.rb:1265:in  
> > `comments’  
> > from (irb):8

Even though you said I should use comments when that didn’t work I  
thought I’d try comment again but I still got an error:

> > p.comment.build(:author =\> ‘me’, :content =\> ‘Whatever content…’)  
> > NoMethodError: undefined method `comment' for #<Posts:0x19673f4> from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/attribute_methods.rb:260:in`method\_missing’  
> > from (irb):9

Am I formatting the parameters for the build method correctly? I didn’t  
know that it mattered what I called my class names… Since I have  
already created them both plural, how would I go about renaming them?  
Would I have to start all over?
