Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使方法在seeds.rb中工作_Ruby On Rails_Ruby On Rails 3_Rake Task - Fatal编程技术网

Ruby on rails 如何使方法在seeds.rb中工作

Ruby on rails 如何使方法在seeds.rb中工作,ruby-on-rails,ruby-on-rails-3,rake-task,Ruby On Rails,Ruby On Rails 3,Rake Task,当lauch$rake db:seeds`时,我有一个错误未定义的方法为#添加朋友。我正在尝试通过rake任务添加朋友,它在用户模型中定义。但这种方法是无法使用的 这是db/seeds.rb文件 require 'faker' require 'populator' User.destroy_all 10.times do user = User.new user.username = Faker::Internet.user_name user.email = Faker::I

当lauch$rake db:seeds`时,我有一个错误
未定义的方法
为#
添加朋友。我正在尝试通过rake任务添加朋友,它在用户模型中定义。但这种方法是无法使用的

这是db/seeds.rb文件

require 'faker'
require 'populator'

User.destroy_all

10.times do
  user = User.new
  user.username = Faker::Internet.user_name
  user.email = Faker::Internet.email
  user.password = "test"
  user.password_confirmation = "test"
  user.save
end


User.all.each do |user|  
  Flit.populate(5..10) do |flit|
    flit.user_id = user.id
    flit.message = Faker::Lorem.sentence
  end

  3.times do
    User.add_friend(User.all[rand(User.count)])
  end
end
还有一个用户文件

class User < ActiveRecord::Base
  # new columns need to be added here to be writable through mass assignment
  attr_accessible :username, :email, :password, :password_confirmation

  attr_accessor :password
  before_save :prepare_password

  validates_presence_of :username
  validates_uniqueness_of :username, :email, :allow_blank => true
  validates_format_of :username, :with => /^[-\w\._@]+$/i, :allow_blank => true, :message => "should only contain letters, numbers, or .-_@"
  validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
  validates_presence_of :password, :on => :create
  validates_confirmation_of :password
  validates_length_of :password, :minimum => 4, :allow_blank => true

  has_many :flits, :dependent => :destroy

  has_many :friendships
  has_many :friends, :through => :friendships



  def add_friend(friend)
    friendship = friendships.build(:friend_id => friend.id)
      if !friendship.save
        logger.debug "User '#{friend.email}' already exists in the user's friendship list."
      end
  end

  # login can be either username or email address
  def self.authenticate(login, pass)
    user = find_by_username(login) || find_by_email(login)
    return user if user && user.password_hash == user.encrypt_password(pass)
  end

  def encrypt_password(pass)
    BCrypt::Engine.hash_secret(pass, password_salt)
  end

  private

  def prepare_password
    unless password.blank?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = encrypt_password(password)
    end
  end
end
class用户true
验证以下内容的格式:username,:with=>/^[-\w\.\u@]+$/i,:allow_blank=>true,:message=>“应仅包含字母、数字或。-\u@”
验证电子邮件的格式:=>/^[-a-z0-9\+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
验证是否存在:password,:on=>:create
验证密码的确认
验证以下内容的长度:password,:minimum=>4,:allow\u blank=>true
有很多:flit,:dependent=>:destroy
有很多:友谊
有很多:朋友,:通过=>:友谊
def添加朋友(朋友)
友谊=友谊。构建(:friend\u id=>friend.id)
如果!友谊,拯救
logger.debug“用户”#{friend.email}已存在于用户的友谊列表中。”
结束
结束
#登录名可以是用户名或电子邮件地址
def自我验证(登录,通过)
user=find_by_用户名(登录)| | find_by_电子邮件(登录)
如果user&&user.password\u hash==user.encrypt\u password(pass),则返回用户
结束
def加密_密码(通过)
BCrypt::Engine.hash\u secret(pass,password\u salt)
结束
私有的
def准备密码
除非密码为空?
self.password\u salt=BCrypt::Engine.generate\u salt
self.password\u hash=加密\u密码(password)
结束
结束
结束

将add\u friend设置为类方法

def self.add_friend(friend)
    friendship = friendships.build(:friend_id => friend.id)
      if !friendship.save
        logger.debug "User '#{friend.email}' already exists in the user's friendship list."
      end
end
或者称之为
User.new.add\u friend(User.all[rand(User.count)])


HTH

使add\u friend成为类方法

def self.add_friend(friend)
    friendship = friendships.build(:friend_id => friend.id)
      if !friendship.save
        logger.debug "User '#{friend.email}' already exists in the user's friendship list."
      end
end
或者称之为
User.new.add\u friend(User.all[rand(User.count)])