Rails将无法正确评估条件的before_save回调

时间:2015-08-20 14:39:16

标签: ruby-on-rails ruby-on-rails-4 model callback conditional

我的应用架构将User.role属性默认为“new”。我的before_save回调的目标是让User模型将user.role属性更改为“admin”,如果正在创建的用户是第一个,否则它将不管它。

User.rb

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
ROLES = %w[admin moderator member new]
  after_save :assign_role

def assign_role
 if self == User.first
  self.role = "admin"
  self.touch
 end
end

def is?(requested_role)
self.role == requested_role.to_s
end
end

Rails控制台

vagrant@vagrant-ubuntu-trusty-64:/vagrant$ rails c
Loading development environment (Rails 4.2.1)
2.1.5 :001 > User.any?
(0.3ms)  SELECT COUNT(*) FROM "users"
=> false
2.1.5 :002 > User.last
User Load (0.4ms)  SELECT  "users".* FROM "users"  ORDER BY "users"."id"     DESC
LIMIT 1
=> #<User id: 1, email: "**************", encrypted_password:  "$
2a$10$FEOjKX1UAZwiEVPbXXW.TOCyh2d7iwIuIRKn8YmYWk....", reset_password_token:  nil
, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1,   curre
nt_sign_in_at: "2015-08-20 14:23:31", last_sign_in_at: "2015-08-20   14:23:31", cu
rrent_sign_in_ip: #<IPAddr: IPv4:10.0.2.2/255.255.255.255>, last_sign_in_ip:  #<I
PAddr: IPv4:10.0.2.2/255.255.255.255>, created_at: "2015-08-20 14:23:31",  update
d_at: "2015-08-20 14:23:31", confirmation_token: nil, confirmed_at: nil,  confirm
ation_sent_at: nil, role: "new">
2.1.5 :003 > User.last
User Load (1.3ms)  SELECT  "users".* FROM "users"  ORDER BY "users"."id"   DESC
LIMIT 1
=> #<User id: 2, email: "***********.com", encrypted_password:  "$2a$10$OtjtlW
BAcrqlws1q5N/ICe6Hjytb0GhKGrVmdcin.Op...", reset_password_token: nil,  reset_pass
word_sent_at: nil, remember_created_at: nil, sign_in_count: 1,  current_sign_in_a
t: "2015-08-20 14:24:44", last_sign_in_at: "2015-08-20 14:24:44",  current_sign_i
n_ip: #<IPAddr: IPv4:10.0.2.2/255.255.255.255>, last_sign_in_ip: #<IPAddr:  IPv4:
10.0.2.2/255.255.255.255>, created_at: "2015-08-20 14:24:44", updated_at:  "2015-
08-20 14:24:44", confirmation_token: nil, confirmed_at: nil,  confirmation_sent_a
t: nil, role: "new">
2.1.5 :004 >

在控制台中创建的第一个用户应该有role:"admin"而不是role:"new"

修改 保存新用户表单

<div class="border-form-div">
<h2 class="text-center">Create a new account</h2>
<%= form_for(resource, :as => resource_name, :url =>   registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>
  <%= f.email_field :email, :autofocus => true, :placeholder => 'Email address' %>
  <%= f.hidden_field :role, :value => "new" %>
  <%= f.password_field :password, :placeholder => 'Password' %>
  <%= f.password_field :password_confirmation, :placeholder => 'Password confirmation' %>
  <%= f.submit "Sign up", :class => 'btn btn-primary center-block' %>
 <% end %>
 </div>

user_controller.rb

class UserController < ApplicationController
before_action :authenticate_user!

params.require(:user).permit(:username, :email, :password,  :encrypted_password, :role)
end

控制器相当空,因为我正在使用设计来处理所有帐户管理

2 个答案:

答案 0 :(得分:0)

before_save {|user| user.role = "admin"}, unless: :user_exists?

def user_exists?
  User.any?
end

答案 1 :(得分:0)

你应该使用User.any?

before_save {self.role = "admin"} unless User.any?

试试这个

after_save :assign_role

def assign_role
 if self == User.first
  self.role = "admin"
  self.touch
 end
end