2012-04-01 8 views
0

사이트에서 오류 테스트 (정확히는 21 개)/오류 메시지가 나타납니다. 사용자 번호에ActiveRecord :: StatementInvalid in Users # show Rails 튜토리얼 12 장

액티브 :: StatementInvalid 보여
보기 rails_projects/sample_app/응용 프로그램 /보기/공유/_stats.html.erb 라인 # 11 제기 여기서 여기

사이트에 메시지입니다 :

SQLite3 :: SQLException : 근처에 "id": 구문 오류 : SELECT COUNT (*) FROM "사용자"INNER JOIN "관계"ON "사용자".id = "relationships".follower_id WHERE ("관계". follows = id = 101))

또한 "추출한 s "또한

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 축소 ource (라인 # 11 정도)).

실패한 테스트에서 유사한 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> 

여기 나의 관계 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 

나는 아직 아무것도 마이그레이션을 삭제하지 않고 (또한 테스트 데이터베이스를 준비) 또 다시 시작,하지만 시도했다.

문제를 해결하기 위해 제공해야하는 다른 파일이 있으면 알려주십시오.

편집

문제는 여기에 내 사용자 모델이었다. 여기에 편집 된 버전 (관련 부분)이다는 foreign_key가 설정된

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 

하기 전에 "followed_ ID"

답변

2

"relationships.followed_"사이가 안 여분의 공간이 그리고 "에 신분증".당신의 견해와 테스트는 이것과 아무런 관련이 없습니다. 당신이 has_and_belongs_to_many 관계를 거기에 정의한다면, 관계 모델이나 사용자 모델에서 실수가 발견되어야합니다. 직접 찾아 낼 수 없다면 여기에 모델의 관련 부분을 게시하십시오.

+0

당신이 옳았습니다! 문제는 내 사용자 모델에있었습니다. 고마워요! – Angel

관련 문제