我正在按照本教程https://www.railstutorial.org/book/following_users#top进行操作,并在步骤12.14。除了添加测试数据之外,我已经按照每个步骤进行操作,因为我正在使用本教程来帮助我学习如何在我自己的项目中开发一个跟随者跟踪功能。
以下是来自我的控制台的错误消息:
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) }
答案 0 :(得分:1)
错误消息解释了自己。 undefined method 'each' for nil:NilClass
告诉您,您正在尝试将each
方法发送到nil
对象。可以推断,您呼叫following
的{{1}}和/或followers
是each
,而不是您希望它的nil
对象
您是否记得在种子文件中创建用户对象?如果您的种子文件与问题中显示的一样,那么它将无效,因为ActiveRecord Relation
模型中没有任何对象。
您必须先创建它们,如here商品详情12.14所示。您既可以使用像Railstutorial那样的FactoryGirl,也可以只使用纯Ruby。