How to show description fields from related table

2019-08-23 03:53发布

Just started playing with Ruby (no IT background) and until now went it quite well. But since two days I'm stuck and don't understand what's going wrong... so many thanks already for helping me out with this problem!! The situation is as described below:

I created a currencymaster table with the following columns: currmasdesc:string, currmasiso:string. I created a currencyrate table with the following columns: currratemasteridd:integer, currratemasteridc:integer, currraterate:decimal, currratedate:date. Whereby the column currratemasteridd reflects the Dominant Currency and the currratemasteridc reflects the Converted Currency to generate combined a currency-pair.

The models/currencymaster.rb looks like this:

class Currencymaster < ActiveRecord::Base
  has_many :CurrencyRateDom, :class_name => "Currencyrate", :foreign_key => "CurrRateMasterIDD" 
  has_many :CurrencyRateConv, :class_name => "Currencrate", :foreign_key => "CurrRateMasterIDC"
end

The models/currencyrate.rb looks like this:

class Currencyrate < ActiveRecord::Base
  belongs_to :CurrencyDominant, :class_name => 'Currencymaster' , :foreign_key => "CurrRateMasterIDD", :validate => true
  belongs_to :CurrencyConverted, :class_name => 'Currencymaster' , :foreign_key => "CurrRateMasterIDC", :validate => true
end

The controllers/currencyrates_controller.rb looks like this:

class CurrencyratesController < ApplicationController
  # GET /currencyrates
  # GET /currencyrates.json
  def index
    @currencyrates = Currencyrate.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @currencyrates }
    end
  end

  # GET /currencyrates/1
  # GET /currencyrates/1.json
  def show
    @currencyrate = Currencyrate.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @currencyrate }
    end
  end
end

Now is my problem that I can't show, in the view/currencyrates/index.html.erb , the related currencymaster.currmasiso instead of the currratemasteridd & currratemasteridc stored in the table currencyrate.

I hope all information is avaibale in this question, else please let me know when other information is needed. Thanks again!

1条回答
欢心
2楼-- · 2019-08-23 04:13

Why you don't follow conventions?

You should create a CurrencyMaster class, don't repeat "currmas" or "currrate" in your column's name. Don't override foreign keys... You code should be like this :

class CurrencyMaster < ActiveRecord::Base
  has_many :currency_rate_doms
  has_many :currency_rate_convs
end

It's the same in your other classes. Rails use the "convention over configuration principe. It would be better after that.

Welcome to wonderful ruby/rails world.

查看更多
登录 后发表回答