has_many and belongs_to relationship error

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

has_many and belongs_to relationship error

bilal
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 %>

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: has_many and belongs_to relationship error

Marc Love
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
>

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: has_many and belongs_to relationship error

bilal
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 %>


--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails