0

Ruby(ITのバックグラウンドなし)で遊び始め、前の質問/回答(リンク)に基づいてプロジェクトを再開しました。私は今、次の状況に陥っています。

次の列を持つcurrencymasterテーブルを作成しました。これにより、ID列はRubyによって作成されます。id:integerdescription:stringisocode:string

次の列を持つcurrencyratesテーブルを作成しまし た。これにより、ID列がRubyによって作成されます。id:integerdominant:integerconverted:integerrate:decimal

このサイトのヘルプに基づいて、次のモデルを作成しました。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.isoCurrencymastercurrencyrate.dominantcurrencyrate.converted

どうもありがとう!!

4

1 に答える 1

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_idあり、クラス名には使用する必要があります。また、を使用する場合は「s」を使用しないでください。converted_idCurrencyRateCurrencyMasterbelongs_to

于 2012-07-26T12:33:09.993 に答える