0

我在网站上收到了一堆失败的测试(准确地说是 21 个)/一条错误消息。

这是网站上的消息:

ActiveRecord::StatementInvalid in Users#show
显示 rails_projects/sample_app/app/views/shared/_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 ))

它还提取了“提取的源代码(在第 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”

4

1 回答 1

2

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

于 2012-04-01T20:44:26.693 回答