我收到了一堆失敗的測試(準確的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」
你說得對!問題出在我的用戶模型中。謝啦! – Angel 2012-04-01 22:35:53