如何显示从相关表中说明字段 [英] How to show description fields from related table

查看:177
本文介绍了如何显示从相关表中说明字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始使用Ruby(没有IT背景)玩,到现在又是相当不错的。但是,由于这两天我很卡,不明白发生了什么错......这么多的感谢已经帮了我这个问题!这种情况的描述如下:

我创建了一个 currencymaster 的表具有以下列: currmasdesc:字符串 currmasiso:字符串。 我用下面的列上创建一个 currencyrate 的表: currratemasteridd:整数 currratemasteridc:整数 currraterate:十进制 currratedate:日期。由此,列 currratemasteridd 体现了主导货币和 currratemasteridc 反映了转换货币,以生成组合的货币对。

该型号/ currencymaster.rb看起来是这样的:

 类Currencymaster<的ActiveRecord :: Base的
  的has_many:Cu​​rrencyRateDom,:将class_name => Currencyrate,:foreign_key => CurrRateMasterIDD
  的has_many:Cu​​rrencyRateConv,:将class_name => Currencrate,:foreign_key => CurrRateMasterIDC
结束
 

该型号/ currencyrate.rb看起来是这样的:

 类Currencyrate<的ActiveRecord :: Base的
  belongs_to的:CurrencyDominant,:将class_name => Currencymaster',:foreign_key => CurrRateMasterIDD:验证=>真正
  belongs_to的:CurrencyConverted,:将class_name => Currencymaster',:foreign_key => CurrRateMasterIDC:验证=>真正
结束
 

该控制器/ currencyrates_controller.rb看起来是这样的:

 类CurrencyratesController<的ApplicationController
  #GET / currencyrates
  #GET /currencyrates.json
  高清指数
    @currencyrates = Currencyrate.all

    respond_to代码做|格式|
      的format.html#index.html.erb
      format.json {渲染JSON:@currencyrates}
    结束
  结束

  #GET / currencyrates / 1
  #GET /currencyrates/1.json
  高清节目
    @currencyrate = Currencyrate.find(PARAMS [:ID])

    respond_to代码做|格式|
      的format.html#show.html.erb
      format.json {渲染JSON:@currencyrate}
    结束
  结束
结束
 

现在是我的问题,我不能显示,在视图/ currencyrates / index.html.erb,而不是相关的 currencymaster.currmasiso 时,在 currratemasteridd &放大器; currratemasteridc 存储在表currencyrate。

我希望在这个问题上的所有信息是avaibale,否则请让我知道,当需要其他信息。再次感谢!

解决方案

为什么你不遵守约定?

您应该创建一个 CurrencyMaster 类,不重复currmas或在您的列的名称currrate。不要忽略外键...您code应该是这样的:

 类CurrencyMaster<的ActiveRecord :: Base的
  的has_many:currency_rate_doms
  的has_many:currency_rate_convs
结束
 

这是在你的其他类一样。导轨使用约定优于配置普林西比,这将是以后更好。

欢迎到精彩的Ruby / Rails社区。

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!

解决方案

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.

这篇关于如何显示从相关表中说明字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆