[英]ActiveRecord::StatementInvalid in Users#show Rails Tutorial Chapter 12
I'm getting a bunch of failing tests (21 to be exact)/ an error message on the site. 我正在网站上收到一堆失败的测试(确切地说是21个)/错误消息。
Here is the message on the site: 这是网站上的消息:
ActiveRecord::StatementInvalid in Users#show 用户#show中的ActiveRecord :: StatementInvalid
Showing rails_projects/sample_app/app/views/shared/_stats.html.erb where line #11 raised: 显示第11行引发的rails_projects / sample_app / app / views / shared / _stats.html.erb:
SQLite3::SQLException: near "id": syntax error: SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users".id = "relationships".follower_id WHERE (("relationships".followed_ id = 101)) SQLite3 :: SQLException:接近“id”:语法错误:SELECT COUNT(*)FROM“users”INNER JOIN“relationships”ON“users”.id =“relationships”.follower_id WHERE((“relationships”.followed_ id = 101 ))
It also pulled up "extracted source (around line #11):" 它还提取了“提取的来源(第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
Also: 也:
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'
When I take away the 当我带走了
<%= pluralize(@user.followers.count, "follower") %>
line, the site works (and my failing tests shrink to 6). 线,网站工作(我的失败测试缩小到6)。
The failing tests seems to have a similar SQL error. 失败的测试似乎有类似的SQL错误。 Here are the failing tests:
以下是失败的测试:
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
Here are the errors (respectively) that they get: 以下是他们得到的错误:
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))
Here is my "_stats.html.erb" file: 这是我的“_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>
Here is my "show.html.erb" file: 这是我的“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>
Here is my relationship db file (if it helps): 这是我的关系db文件(如果它有帮助):
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
I tried deleting the migration and starting over again (also preparing the test database), but still nothing. 我尝试删除迁移并重新开始(也准备测试数据库),但仍然没有。
If there are any other files that I should supply to help solve the problem, let me know! 如果有任何其他文件我应该提供以帮助解决问题,请告诉我!
EDIT 编辑
The problem here was my User model. 这里的问题是我的用户模型。 Here is the edited version (relevant portion):
这是编辑后的版本(相关部分):
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
Before, the foreign_key was set to "followed_ id" 之前,foreign_key被设置为“follow_ id”
There's an extra space that shouldn't be there between "relationships.followed_" and "id". 在“relationships.followed_”和“id”之间不应该有一个额外的空间。 Your views and tests have nothing to do with this, the mistake should be found in your Relationship model or User model if you define the has_and_belongs_to_many-relationship there.
您的视图和测试与此无关,如果您在那里定义has_and_belongs_to_many关系,则应在您的关系模型或用户模型中找到错误。 If you can't spot it yourself, post the relevant part of your models here.
如果您无法自己发现,请在此处发布模型的相关部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.