在 Rails 中反转与 ActiveRecord 的 has_one 关系?
我要在这里拔掉我的头发。
我有以下两个表:
databases
---------
id
user_id
driver
host
port
database_name
username_encryption_id
password_encryption_id
encryptions
-----------
id
encryption
salt
使用 Ruby on Rails ActiveRecord 关联,我希望能够做到这一点:
database = Database.find_by_id(database_id)
encryption = database.username_encryption
我很确定这是一个 has_one 关系(数据库有一个用户名加密)。这些都不起作用:
has_one :password_encryption, :class_name => "Encryption", :foreign_key => :password_encryption_id, :inverse_of => :encryption
has_one :username_encryption, :inverse_of => :database
我如何让它发挥作用?我快死在这里了。
I'm going to pull my hair out here.
I have the following two tables:
databases
---------
id
user_id
driver
host
port
database_name
username_encryption_id
password_encryption_id
encryptions
-----------
id
encryption
salt
Using a Ruby on Rails ActiveRecord association, I want to be able to do this:
database = Database.find_by_id(database_id)
encryption = database.username_encryption
I'm pretty sure this is a has_one relationship (database has one username encryption). None of these work:
has_one :password_encryption, :class_name => "Encryption", :foreign_key => :password_encryption_id, :inverse_of => :encryption
has_one :username_encryption, :inverse_of => :database
How do I get this working? I'm dying here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来像一个
belongs_to
关联,因为database
具有username_encryption_id
:This looks like a
belongs_to
association, since thedatabase
has theusername_encryption_id
: