Asociation problems

Hi,

I’m trying to develop a simple blog aplication, with no scaffolding
help. I create the model post and comment, and stablished the
asociations:

class Post < ActiveRecord::Base
has_many :comments
validates_presence_of :title, :poster, :content
end

class Comment < ActiveRecord::Base
belongs_to :post
end

But, when I try to get the comments in a post I get error:

SQLite3::SQLException: no such column: comments.post_id: SELECT * FROM
“comments” WHERE (“comments”.post_id = 18)

Extracted source (around line #7):

4:

Commenter
5: Comment
6:
7: <% for comment in @comments %>
8: <%=h comment.commenter %>
9: <%=h comment.comment %>

No such column? Error in @comments, here is the controller:

def showcomments
@post = Post.find(params[:id])
@comments = @post.comments
end

I don’t know where the problem is :S

Thanks for your answers

Hi

class Comment < ActiveRecord::Base
belongs_to :post
end

This expects post_id column in comments table. Is it there?

Sijo

Does the table comments have a column post_id? A belongs_to and
has_many relationship is by default always linked in the child object
with the parent’s id!

On 29 jul, 11:08, Protos J. [email protected]

Protos J. wrote:

I thought the relation belongs_to automatically creates this column.

No such luck. It expects it to be in the DB already. So write a
migration and you should be OK.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I thought the relation belongs_to automatically creates this column.

This is getting me crazy:

Error:

You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

/home/diego/blog/app/controllers/myblog_controller.rb:68:in
`createcomment’

Action Controller:

def createcomment
@post = Post.find(params[:id])
@comment = @post.comments.build
@comment.comment = params[:comment][:comment]
@comment.commenter = params[:comment][:commenter]
redirect_to :url => {:action => ‘showpost’, :id => @post.id}
end

So the problem is in params, it is nil… Form:

New Comment

<% form_for :comment, @comment, :url => {:action => ‘createcomment’,
:id => @post.id} do |f| %>

<%= f.label :commenter%>
<%= f.text_field :commenter%>

<%= f.label :comment%>
<%= f.text_area :comment%>

<%= f.submit "Comentar"%>

<% end %>

I see… I did the migration then, thank you (thanks to rails for the
migrations these problems with db are quickly fixed ^^).

But I have the problem with the form for comments:

Error:

You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

/home/diego/blog/app/controllers/myblog_controller.rb:68:in
`createcomment’

Action Controller:

def createcomment
@post = Post.find(params[:id])
@comment = @post.comments.build
@comment.comment = params[:comment][:comment]
@comment.commenter = params[:comment][:commenter]
redirect_to :url => {:action => ‘showpost’, :id => @post.id}
end

So the problem is in params, it is nil… Form:

New Comment

<% form_for :comment, @comment, :url => {:action => ‘createcomment’,
:id => @post.id} do |f| %>

<%= f.label :commenter%>
<%= f.text_field :commenter%>

<%= f.label :comment%>
<%= f.text_area :comment%>

<%= f.submit "Comentar"%>

<% end %>

2009/7/29 Protos J. [email protected]:

I thought the relation belongs_to automatically creates this column.

A relationship tells Rails to expect the relevant id column, not to
create it. The only way that the db structure will change is if you
provide a migration (unless you change it manually of course).

Colin

2009/7/29 Protos J. [email protected]:

The error occurred while evaluating nil.[]
@comment.commenter = params[:comment][:commenter]
Perhaps params[:comment] is nil?
Have a look in the log to see what parameters have been posted with the
form.
In this situation if I still cannot see the problem I would use
ruby-debug to break in and look at the variables.

redirect_to :url => {:action => ‘showpost’, :id => @post.id}
end

So the problem is in params, it is nil… Form:

New Comment

 <% form_for :comment, @comment, :url => {:action => ‘createcomment’,
:id => @post.id} do |f| %>

Are you sure the syntax here is correct? Are you sure you need
:comment and @comment in form_for?

Colin L. wrote:

2009/7/29 Protos J. [email protected]:

The error occurred while evaluating nil.[]
@comment.commenter = params[:comment][:commenter]
Perhaps params[:comment] is nil?
Have a look in the log to see what parameters have been posted with the
form.
In this situation if I still cannot see the problem I would use
ruby-debug to break in and look at the variables.

redirect_to :url => {:action => ‘showpost’, :id => @post.id}
end

So the problem is in params, it is nil… Form:

New Comment

 <% form_for :comment, @comment, :url => {:action => ‘createcomment’,
:id => @post.id} do |f| %>

Are you sure the syntax here is correct? Are you sure you need
:comment and @comment in form_for?

Yes, the error is because params[:comment] is nil, but it doesn’t have
to! I think form is ok. HTML generated by this form:

Commenter

Comment

I see no error.

I tried creating comments by console and it works, the problem seems to
be with the form, but I don’t find it.

thx

Hi
This be approached in railsway You have post has_many comments and
comment belong to post ?

So can create two controllers Posts controller and Comments controller
And in app/controllers/posts_controller.rb add

def show
@post = Post.find(params[:id])
@comment = @post.comments.new
end

Now the view app/views/posts/show.html.erb

<% form_for [@post, @comment] do |f| %>

<%= f.label :commenter%>
<%= f.text_field :commenter%>

<%= f.label :comment%>
<%= f.text_area :comment%>

<%= f.submit "Comentar"%>

<% end %>

Then in app/controllers/comments_controller.rb

def create
@post = Post.find(params[:post_id])
@post.comments.new(params[:comment]).save
redirect_to(post_path(@post))
end

     edit routes.rb

map.resources :posts, :has_many => [:comments]

 Thts all And one more you can move this two controllers to a 

namespace Blog
This can be done by
./script/generate controller Blog::Posts
./script/generate controller Blog::Comments

So the changes at three places above They are

<% form_for [:blog,@post, @comment] do |f| %>

        and

redirect_to(blog_post_path(@post))

    and

map.namespace(:blog) do |b|
b.resources :posts, :has_many => [:comments]
end

Sijo