Forum: Ruby on Rails has_many and belongs_to relationship error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
65ad44a1eb0bb7ba948e4ec50abe6cb6?d=identicon&s=25 Bilal Hameed (saffire)
on 2005-12-23 06:54
Hi i am getting following error

 ActiveRecord::StatementInvalid in Blog#index

Showing app/views/blog/index.rhtml where line #10 raised:

Mysql::Error: #42S22Unknown column 'posts.user_id' in 'where clause':
SELECT * FROM posts WHERE (posts.user_id = 4)

Extracted source (around line #10):

7: <div class="separator">&nbsp;</div>
8: <% end %>
9:
10: <% for post in @posts %>
11: <div class="mission">
12: <h3><%= h(post.posttitle) %></h3>
13: <%= post.posttext %>

The code follows:

class Post < ActiveRecord::Base
	belongs_to :user


end

class User < ActiveRecord::Base
	has_many :posts
end

class BlogController < ApplicationController


  def index
	  @user = User.find_by_login('bilal')
	  @posts = @user.posts
	  @missions = Mission.find(:all)
  end
end

index.rhtml is
<% for mission in @missions %>
<div class="mission">
<h3><%= h(mission.missionname) %></h3>
<%= mission.missiondescription %>
</div>
<div class="separator">&nbsp;</div>
<% end %>

<% for post in @posts %>
<div class="mission">
<h3><%= h(post.posttitle) %></h3>
<%= post.posttext %>
</div>
<div class="separator">&nbsp;</div>
<% end %>
137039860fa6b38713e1349da8a4137d?d=identicon&s=25 Marc Love (Guest)
on 2005-12-23 08:12
I would get rid of @posts = @user.posts from your controller and change
the line 10 in your view to <% for post in @user.posts %>.  There's
really no need to set the @posts instance variable, and I don't know why
but it seems its messing up your SQL statement.

bilal hameed wrote:
> Hi i am getting following error
>
>  ActiveRecord::StatementInvalid in Blog#index
>
> Showing app/views/blog/index.rhtml where line #10 raised:
>
> Mysql::Error: #42S22Unknown column 'posts.user_id' in 'where clause':
> SELECT * FROM posts WHERE (posts.user_id = 4)
>
> Extracted source (around line #10):
>
> 7: <div class="separator">&nbsp;</div>
> 8: <% end %>
> 9:
> 10: <% for post in @posts %>
> 11: <div class="mission">
> 12: <h3><%= h(post.posttitle) %></h3>
> 13: <%= post.posttext %>
>
> class BlogController < ApplicationController
>
>
>   def index
> 	  @user = User.find_by_login('bilal')
> 	  @posts = @user.posts
> 	  @missions = Mission.find(:all)
>   end
> end
>
65ad44a1eb0bb7ba948e4ec50abe6cb6?d=identicon&s=25 Bilal Hameed (saffire)
on 2005-12-23 18:16
Thanks marc. I have made the changes you suggested but the error remains
as is.

The blog controller and the index.rhtml looks as follows now

class BlogController < ApplicationController
  def index
	  @user = User.find_by_login('bilal')
	  @missions = Mission.find(:all)
  end
end

<% for mission in @missions %>
<div class="mission">
<h3><%= h(mission.missionname) %></h3>
<%= mission.missiondescription %>
</div>
<div class="separator">&nbsp;</div>
<% end %>

<% for post in @user.posts %>
<div class="mission">
<h3><%= h(post.posttitle) %></h3>
<%= post.posttext %>
</div>
<div class="separator">&nbsp;</div>
<% end %>
This topic is locked and can not be replied to.