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