开发者

Reverse has_one relationship with ActiveRecord in Rails?

开发者 https://www.devze.com 2023-03-11 16:56 出处:网络
I\'m going to pull my hair out here. I have the following two tables: databases --------- id user_id driver

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)
encryp开发者_如何学编程tion = 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.


This looks like a belongs_to association, since the database has the username_encryption_id:

class UsernameEncryption < Encryption
  has_one :database
end

class Database
  belongs_to :username_encryption
end
0

精彩评论

暂无评论...
验证码 换一张
取 消