在 Rails 中查询 Devise users 表

发布于 2024-11-09 10:13:35 字数 970 浏览 1 评论 0原文

我刚刚设置了 Devise auth 库,但我对如何查询它感到困惑。

例如,之前当我制作自己的 users 表时,我制作了这个 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

我在这里哪里出错了?

I just set up the Devise auth library and I am confused about how it is meant to be queried.

For example, before when I made my own users table, I made this user.rb file

class User < ActiveRecord::Base
  set_primary_key:uid
end

And I was able to query by uid like this:

@User = User.find(1)
print @User   

Now that Devise added a number of columns and the users.rb file looks like this:

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

The above code to query this table does not work and I am not sure how it is meant to be queried.

Also, I am not sure if I should do something like this

rails generate devise User

Where am I going wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分开我的手 2024-11-16 10:13:35

我认为覆盖 Rails 默认主键不是一个好习惯,也许您可​​以尝试通过以下方式查找用户:

@User = User.find_by_uid(1)

I don't think it's a good practice to override the Rails default primary keys, perhaps you could try finding your users the following way instead:

@User = User.find_by_uid(1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文