如何显示相关表格中的描述字段(2)

时间:2012-07-26 10:49:07

标签: ruby-on-rails activerecord

刚开始使用Ruby(没有IT背景)并根据之前的问题/答案(link)重新启动项目。我现在有以下情况:

创建了一个 currencymaster 表,其中包含以下列id:integerdescription:stringisocode:string,其中ID列由Ruby创建。

创建了一个 currencyrates 表,其中包含以下列id:integerdominant:integerconverted:integer以及rate:decimal,其中ID列由Ruby创建。

基于help on this site我创建了以下模型。 models / currencymaster.rb 如下所示:

class Currencymaster < ActiveRecord::Base
  has_many :currency_rates_dominant, :validate => true, :class_name => 'Currencyrate' 
  has_many :currency_rates_converted, :validate => true, :class_name => 'Currencyrate' 
end

models / currencyrate.rb如下所示:

class Currencyrate < ActiveRecord::Base
  belongs_to :currency_master_doms, :class_name => 'Currencymaster' 
  belongs_to :currency_master_convs, :class_name => 'Currencymaster' 
end

我在两个控制器中都没有改变任何东西。

views \ currencyrates \ index.html.erb 是通过Ruby自动生成的,并将记录的值显示为整数。目标是在currencymaster.isoCurrencymaster

currencyrate.dominant表格中显示currencyrate.converted

非常感谢!!

1 个答案:

答案 0 :(得分:0)

我认为你应该改变你的课程:

class Currencyrate < ActiveRecord::Base
  belongs_to :currency_master_dom, :class_name => 'Currencymaster', :foreign_key => 'dominant' 
  belongs_to :currency_master_conv, :class_name => 'Currencymaster' , :foreign_key => 'converted'
end

之后你应该这样做:

rate = Currencyrate.first
rate.currency_master_dom.iso
rate.currency_master_conv.iso

不遵守所有惯例。你应该使用dominant_idconverted_id作为forign键,CurrencyRateCurrencyMaster作为类名,你不应该使用belongs_to

相关问题