Relationships not working...

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...

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

Frederick Cheung 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?