我刚刚设置了Devise auth库,我很困惑它是如何被查询的。
例如,在我创建自己的用户表之前,我创建了这个user.rb文件
class User < ActiveRecord::Base
set_primary_key:uid
end
我能够通过uid查询:
@User = User.find(1)
print @User
现在,Devise添加了许多列,users.rb文件如下所示:
class Users < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:first_name, :last_name, :organization_name
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
查询此表的上述代码不起作用,我不确定它是如何被查询的。
另外,我不确定我是否应该做这样的事情
rails generate devise User
我在哪里错了?
答案 0 :(得分:2)
我认为覆盖Rails默认主键不是一个好习惯,也许您可以尝试以下列方式查找用户:
@User = User.find_by_uid(1)