一个表中的多个外键

时间:2014-07-30 13:54:22

标签: ruby-on-rails-3 ruby-on-rails-3.2

我有两个名为' Event' &安培; '货币'

在事件模型中,相应字段为id,prize_pool,title,name,currency_id,prize_pool_currency_id,在货币模型中,其字段为id,currency。

Event.rb

class Event < ActiveRecord::Base
attr_accessible :prize_pool,:title,:name,:currency_id,:prize_pool_currency_id
belongs_to :buy_currency, :class_name => 'Currency', :foreign_key => 'currency_id'
  belongs_to :prize_pool_currency, :class_name => 'Currency', :foreign_key => 'prize_pool_currency_id'
end

Currency.rb

class Currency < ActiveRecord::Base
  attr_accessible :currency
  has_one :event
  has_one :event_player
end

当我们访问控制器时,我发现了这个错误:

&#34;名为&#39;货币的协会&#39;没找到;也许你拼错了?&#34;

如何访问或关联彼此以便轻松访问所有外键?

1 个答案:

答案 0 :(得分:0)

您可以使用您为关联指定的名称(而不是类名称)来访问关联。在您的情况下,它将是buy_currencyprize_pool_currency

@poker_result = Event.includes(:buy_currency)