2014-10-30 110 views
0

我正在關注本教程https://www.railstutorial.org/book/following_users#top,並且在步驟12.14。除了添加測試數據之外,我已經執行了每一步,因爲我正在使用本教程來幫助我學習如何在自己的項目上開發追隨者以下功能。Ruby on Rails rake db:在rails教程中未定義的種子教程

這裏是我的控制檯錯誤信息:

rake aborted! 
NoMethodError: undefined method `each' for nil:NilClass 
/Users/alexanderkehaya/Desktop/wewrite/db/seeds.rb:13:in 

任何想法,我做錯了/失蹤? 編輯:如何讓種子數據成功耙取?

感謝您的幫助。

這裏是我的數據:

ActiveRecord::Schema.define(version: 20141030023857) do 

create_table "collaborators_stories", id: false, force: true do |t| 
t.datetime "created_at" 
t.datetime "updated_at" 
t.integer "collaborator_id" 
t.integer "story_id" 
end 

create_table "lines", force: true do |t| 
t.string "text" 
t.integer "score",   default: 0 
t.integer "depth",   default: 0 
t.integer "previous_line_id" 
t.datetime "created_at" 
t.datetime "updated_at" 
t.integer "user_id" 
t.integer "story_id" 
end 

create_table "relationships", force: true do |t| 
t.integer "follower_id" 
t.integer "followed_id" 
t.datetime "created_at", null: false 
t.datetime "updated_at", null: false 
end 

add_index "relationships", ["followed_id"], name: "index_relationships_on_followed_id" 
add_index "relationships", ["follower_id", "followed_id"], name:  "index_relationships_on_follower_id_and_followed_id", unique: true 
add_index "relationships", ["follower_id"], name: "index_relationships_on_follower_id" 

create_table "stories", force: true do |t| 
t.datetime "created_at" 
t.datetime "updated_at" 
end 

create_table "users", force: true do |t| 
t.string "email",     default: "", null: false 
t.string "encrypted_password",  default: "", null: false 
t.string "reset_password_token" 
t.datetime "reset_password_sent_at" 
t.datetime "remember_created_at" 
t.integer "sign_in_count",   default: 0, null: false 
t.datetime "current_sign_in_at" 
t.datetime "last_sign_in_at" 
t.string "current_sign_in_ip" 
t.string "last_sign_in_ip" 
t.datetime "created_at" 
t.datetime "updated_at" 
t.string "provider" 
t.string "uid" 
t.string "name" 
t.string "profile_image_url" 
end 

add_index "users", ["email"], name: "index_users_on_email", unique: true 
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 

end 

這裏是我的user.rb文件:

class User < ActiveRecord::Base 
# Include default devise modules. Others available are: 
# :confirmable, :lockable, :timeoutable and :omniauthable 
devise :database_authenticatable, :omniauthable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 

has_many :lines 
has_many :active_relationships, class_name: "Relationship", 
           foreign_key: "follower_id", 
           dependent: :destroy 
has_many :passive_relationships, class_name: "Relationship", 
           foreign_key: "followed_id", 
           dependent: :destroy 
has_many :following, through: :active_relationships, source: :followed 
has_many :followers, through: :passive_relationships, source: :follower 
has_and_belongs_to_many :stories, :foreign_key => :collaborator_id, :join_table => :collaborators_stories 

def profile_image_uri(size = "mini") 
    # parse_encoded_uri(insecure_uri(profile_image_uri_https(size))) unless @attrs[:profile_image_url_https].nil? 
    unless self.provider == "facebook" 
     self.profile_image_url.sub! "normal", size unless self.profile_image_url.nil? 
    else 
     self.profile_image_url 
    end 
end 

#Follows a user. 
def follow(other_user) 
    active_relationships.create(followed_id: other_user.id) 
end 

# Unfollows a user. 
def unfollow(other_user) 
    active_relationships.find_by(followed_id: other_user.id).destroy 
end 

# Returns true if the current user is following the other user. 
def following?(other_user) 
    following.include?(other_user) 
end 
def self.find_for_twitter_oauth(auth, signed_in_resource=nil) 


    user = User.where(:provider => auth.provider, :uid => auth.uid).first 

    if user 

     if user.profile_image_url != auth.extra.raw_info.profile_image_url 
     user.update_attribute(:profile_image_url, auth.extra.raw_info.profile_image_url) 
     end 

     return user 

    else 
     registered_user = User.where(:email => auth.uid + "@twitter.com").first 

     if registered_user 

     return registered_user 
     else 

     user = User.create(name:auth.extra.raw_info.name, 
          provider:auth.provider, 
          profile_image_url:auth.extra.raw_info.profile_image_url, 
          uid:auth.uid, 
          email:auth.uid+"@twitter.com", 
          password:Devise.friendly_token[0,20], 
         ) 
     end 
    end 
    end 

    def self.find_for_facebook_oauth(auth, signed_in_resource=nil) 

    user = User.where(:provider => auth.provider, :uid => auth.uid).first 
    if user 

     if user.profile_image_url != auth.info.image 
     user.update_attribute(:profile_image_url, auth.info.image) 
     end 

     return user 
    else 
     registered_user = User.where(:email => auth.info.email).first 
     if registered_user 
     return registered_user 
     else 
     user = User.create(name:auth.extra.raw_info.name, 
          provider:auth.provider, 
          profile_image_url:auth.info.image, 
          uid:auth.uid, 
          email:auth.info.email, 
          password:Devise.friendly_token[0,20], 
         ) 
    end 
end 
end 
end 

這裏是我的relationships.rb文件:

class Relationship < ActiveRecord::Base 
belongs_to :follower, class_name: "User" 
belongs_to :followed, class_name: "User" 
validates :follower_id, presence: true 
validates :followed_id, presence: true 
end 

編輯:我添加我的種子文件:

users = User.all 
user = users.first 
following = users[2..50] 
followers = users[3..40] 
following.each { |followed| user.follow(followed) } 
followers.each { |follower| follower.follow(user) } 
+1

什麼是您的問題 – anusha 2014-10-30 03:28:52

+0

剛剛在頂部添加了一個編輯。問題是:我如何擺脫錯誤?一般來說,我是rails /編碼新手。不知道在哪裏尋找問題。 @anusha – user3787971 2014-10-30 03:34:01

+0

錯誤是因爲您從'seed'文件中刪除該文件後沒有任何記錄並嘗試檢查'user'表是否獲取其中的記錄 – anusha 2014-10-30 03:41:20

回答

1

錯誤信息解釋了它自己。 undefined method 'each' for nil:NilClass告訴您,您正在嘗試將each方法發送給nil對象。可以推斷,following和/或followers,你打電話給each,是nil,而不是你希望的ActiveRecord Relation對象。

您是否記得在種子文件中創建用戶對象?如果您的種子文件與您的問題中顯示的一樣,則它不起作用,因爲User模型在其中沒有任何對象。

您必須先創建它們,如here清單12.14所示。您可以像使用Railstutorial一樣使用FactoryGirl,或者使用Ruby。

+0

@ user3787971如果要使用FactoryGirl,請將其添加到您的應用程序中。底線是您必須在這些代碼可以工作之前生成並保留一些記錄給用戶。 – 2014-10-30 04:17:03

+0

感謝@MikeC這是正確的方向。我想通了,學到了很多東西。 – user3787971 2014-10-30 06:46:49