Ruby on rails Rails协会没有';t激发Postgres查询并返回Nil
我有两个Ruby on rails Rails协会没有';t激发Postgres查询并返回Nil,ruby-on-rails,postgresql,foreign-keys,has-many,Ruby On Rails,Postgresql,Foreign Keys,Has Many,我有两个与一个模型有许多关联。一个关联工作正常并启动查询,另一个只返回nil。 我正在使用一个只读的远程postgres数据库 以下是模型: #stack_address.rb 类StakeAddress
与一个模型有许多关联。一个关联工作正常并启动查询,另一个只返回nil
。
我正在使用一个只读的远程postgres数据库
以下是模型:
#stack_address.rb
类StakeAddress<应用程序记录
self.table\u name='stack\u address'
有很多:奖励,外键::地址id
有很多:代表团、外键::地址id
有很多:utxo视图、外键::桩号地址\u id
结束
#pool_hash.rb
类PoolHash
下面是我测试关联时发生的情况:
$ rails c
Running via Spring preloader in process 68412
Loading development environment (Rails 6.0.3.4)
2.6.1 :001 > d = Delegation.all.first
Delegation Load (2.3ms) SELECT "delegation".* FROM "delegation" ORDER BY "delegation"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> #<Delegation id: 1, addr_id: 61, cert_index: 1, pool_hash_id: 1, active_epoch_no: 210, tx_id: 2424635>
2.6.1 :002 > d.stake_address
=> nil
2.6.1 :003 > d.pool_hash
PoolHash Load (2.3ms) SELECT "pool_hash".* FROM "pool_hash" WHERE "pool_hash"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
=> #<PoolHash id: 1, hash_raw: "\x158\x06\xDB\xCD\x13M\xDE\xE6\x9A\x8CR\x04\xE3\x8A\xC8\x04H\xF6#B\xF8\xC2<\xFEK~\xDF", view: "pool1z5uqdk7dzdxaae5633fqfcu2eqzy3a3rgtuvy087fdld7...">
问题似乎在于您的委派
表。它没有stack\u address\u id
列,而是外键驻留在addr\u id
中。因此,请尝试:
class Delegation < ApplicationRecord
self.table_name = 'delegation'
belongs_to :stake_address, foreign_key: "addr_id"
belongs_to :pool_hash
end
类委托
我明白了,我告诉过在stack\u address.rb中有很多:代表团、外键::addr\u id
?在协会的双方都需要它?是的,似乎在调用StakeAddress上的关联时使用了该键,如StakeAddress.delegations
,但在调用Delegation
模型上的关联时不适用,需要单独定义。
class Delegation < ApplicationRecord
self.table_name = 'delegation'
belongs_to :stake_address, foreign_key: "addr_id"
belongs_to :pool_hash
end