简体   繁体   中英

How to show description fields from related table (2)

Just started playing with Ruby (no IT background) and restarted the project based on previous question / answer ( link ). I have got the following situation now:

created a currencymaster table with the following columns id:integer , description:string and isocode:string whereby the ID column is created by Ruby.

created a currencyrates table with the following columns id:integer , dominant:integer and converted:integer and rate:decimal whereby the ID column is created by Ruby.

Based on help on this site I created the following models. The models/currencymaster.rb looks like this:

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

The models/currencyrate.rb looks like this:

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

I haven't changed anything yet in the both controllers.

The views\\currencyrates\\index.html.erb is generated automatically via Ruby and is showing the values of the records as integer. The goal is to show the currencymaster.iso value out of the Currencymaster table for both currencyrate.dominant and currencyrate.converted

Thanks a lot!!

I think you should change your class like this :

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

After that you should do this :

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

All the conventions aren't respected. You should use dominant_id and converted_id as forign keys, CurrencyRate and CurrencyMaster for class names and you shouldn't have a 's' whan you use belongs_to .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM