2012-04-01 194 views
0

我收到了一堆失败的测试(准确的21)/网站上的错误消息。ActiveRecord :: StatementInvalid在用户#show Rails教程第12章

这里是在网站上的消息:在用户#

的ActiveRecord :: StatementInvalid显示
显示rails_projects/sample_app /应用/视图/共享/ _stats.html.erb其中线#11提出: SQLite3 :: SQLException:在“id”附近:语法错误:SELECT COUNT(*)FROM“users”INNER JOIN“relationships”ON“users”.id =“relationships”.follower_id WHERE((“relationships”。 followed_id = 101))

它也拉起“提取s乌尔斯河(大约线#11):”

8: </span> </a> 
9: </td> <td> 
10: <a href="<%= followers_user_path(@user) %>"> <span id="followers" class="stat"> 
11: <%= pluralize(@user.followers.count, "follower") %> </span> 
12: </a> </td> 
13:  </tr> 
14: </table> 

Trace of template inclusion: app/views/users/show.html.erb 

另外:

app/views/shared/_stats.html.erb:11:in `_app_views_shared__stats_html_erb__1272198506242050260_70324836955760_711623751783882131' 
app/views/users/show.html.erb:22:in `_app_views_users_show_html_erb___2566196705224179076_70324816563400__997253365199883939' 

当我带走

<%= pluralize(@user.followers.count, "follower") %> 

线路,站点工作(和我的测试失败降至6 )。

失败的测试似乎有一个类似的SQL错误。下面是失败的测试:

rspec ./spec/controllers/users_controller_spec.rb:313 # UsersController DELETE 'destroy' as an admin user should destroy the user 
rspec ./spec/controllers/users_controller_spec.rb:319 # UsersController DELETE 'destroy' as an admin user should redirect to the users page 
rspec ./spec/controllers/users_controller_spec.rb:355 # UsersController follow pages when signed in should show user followers 
rspec ./spec/models/user_spec.rb:185 # User micropost associations should destroy associated microposts 
rspec ./spec/models/user_spec.rb:265 # User relationships should include the follower in the followers array 

这里是错误(分别),他们得到:

1. Failure/Error: delete :destroy, :id => @user 
ActiveRecord::StatementInvalid: 
    SQLite3::SQLException: near "id": syntax error: SELECT "relationships".* FROM "relationships" WHERE ("relationships".followed_ id = 1) 
2. Failure/Error: delete :destroy, :id => @user 
ActiveRecord::StatementInvalid: 
    SQLite3::SQLException: near "id": syntax error: SELECT "relationships".* FROM "relationships" WHERE ("relationships".followed_ id = 1) 
3. Failure/Error: get :followers, :id => @other_user 
ActiveRecord::StatementInvalid: 
    SQLite3::SQLException: near "id": syntax error: SELECT "users".* FROM "users" INNER JOIN "relationships" ON "users".id = "relationships".follower_id WHERE (("relationships".followed_ id = 2)) LIMIT 30 OFFSET 0 
4. Failure/Error: @user.destroy 
ActiveRecord::StatementInvalid: 
    SQLite3::SQLException: near "id": syntax error: SELECT "relationships".* FROM "relationships" WHERE ("relationships".followed_ id = 1) 
5. Failure/Error: @followed.followers.should include(@user) 
ActiveRecord::StatementInvalid: 
    SQLite3::SQLException: near "id": syntax error: SELECT "users".* FROM "users" INNER JOIN "relationships" ON "users".id = "relationships".follower_id WHERE (("relationships".followed_ id = 2)) 

这里是我的 “_stats.html.erb” 文件:

<% @user ||= current_user %> <div class="stats"> 
<table summary="User stats"> 
<tr> 
    <td> 
     <a href="<%= following_user_path(@user) %>"> 
      <span id="following" class="stat"> 
      <%= @user.following.count %> following 
     </span> 
     </a> 
    </td> 
    <td> 
     <a href="<%= followers_user_path(@user) %>"> 
      <span id="followers" class="stat"> 
      <%= pluralize(@user.followers.count, "follower") %> 
      </span> 
      </a> 
     </td> 
    </tr> 
</table> 

我的“show.html.erb”文件:

<table summary="Information about following/followers"> 
<tr> 
    <td class="main"> 
     <h1><%= @title %></h1> 

     <% unless @users.empty? %> 
      <ul class="users"> 
       <%= render @users %> 
      </ul> 
      <%= will_paginate @users %> 
     <% end %> 
    </td> 
    <td class="sidebar round"> 
     <strong>Name</strong> <%= @user.name %><br/> 
     <strong>URL</strong> <%= link_to user_path(@user), @user %><br/> 
     <strong>Microposts</strong> <%= @user.microposts.count%> 
     <%= render 'shared/stats' %> 
     <% unless @users.empty? %> 
      <% @users.each do |user| %> 
       <%= link_to gravatar_for(user, :size => 30), user%> 
      <% end %> 
     <% end %> 
    </td> 
</tr> 

这里是我的关系数据库文件(如果它帮助):

class CreateRelationships < ActiveRecord::Migration 
    def self.up 
create_table :relationships do |t| 
    t.integer :follower_id 
    t.integer :followed_id 

    t.timestamps 
end 
    add_index :relationships, :follower_id 
    add_index :relationships, :followed_id 
    add_index :relationships, [:follower_id, :followed_id], :unique => true 
end 

    def self.down 
    drop_table :relationships 
end 
end 

我试图删除的迁移,并再次重新开始(也准备测试数据库),但仍然一无所获。

如果还有其他文件可以帮助我解决问题,请告诉我!这里

编辑

问题是我的用户模型。下面是编辑后的版本(相关部分):

class User < ActiveRecord::Base 
attr_accessor :password 
attr_accessible :name, :email, :password, :password_confirmation 

has_many :microposts, :dependent => :destroy 
has_many :relationships, :foreign_key => "follower_id", 
         :dependent => :destroy 
has_many :following, :through => :relationships, :source => :followed 
has_many :reverse_relationships, :foreign_key => **"followed_id"**, 
           :class_name => "Relationship", 
           :dependent => :destroy 
has_many :followers, :through => :reverse_relationships, :source => :follower 

之前,foreign_key被设置为“followed_ ID”

回答

2

有一个额外的空间,不应该“relationships.followed_”之间是有和“ ID”。您的观点和测试与此无关,如果您在那里定义has_and_belongs_to_many关系,则应在关系模型或用户模型中找到该错误。如果您无法自己发现,请在此处发布模型的相关部分。

+0

你说得对!问题出在我的用户模型中。谢啦! – Angel 2012-04-01 22:35:53

相关问题