Rails测试弃用警告

时间:2014-02-01 17:08:56

标签: unit-testing ruby-on-rails-4

Rails测试在我测试模型时引入了以下警告。我应该对我的模型进行哪些修改,

rake test test/models/user.rb
[RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.
DEPRECATION WARNING: The following options in your User.has_many :incoming_friends declaration are deprecated: :conditions. Please use a scope block instead. For example, the following:

has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment'

should be rewritten as the following:

has_many :spam_comments, -> { where spam: true }, class_name: 'Comment'
. (called from <class:User> at /home/divya/projects/shufflejoy/app/models/user.rb:9)

这是我的用户模型,

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
validates :user_name , :email, :first_name ,:last_name , :presence => true
has_many :invitations
has_many :incoming_friends , :class_name => "User" , :foreign_key=>"friend_id" , :through => :invitations, :conditions => ["status = 1"]
has_many :outgoing_friends , :class_name => "User" , :foreign_key=>"user_id", :through => :invitations, :conditions => ["status = 1"]

end

1 个答案:

答案 0 :(得分:0)

您需要将关联条件写为可调用对象。像这样:

has_many :incoming_friends, -> { where(:status => '1') }, :class_name => "User", :foreign_key => "friend_id", :through => :invitations
has_many :outgoing_friends, -> { where(:status => '1') }, :class_name => "User", :foreign_key => "user_id", :through => :invitations

官方Rails文档中的更多信息:http://guides.rubyonrails.org/association_basics.html#scopes-for-has-many