Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 如何显示相关表中的说明字段_Ruby On Rails 3_Activerecord_Associations_Multiple Tables - Fatal编程技术网

Ruby on rails 3 如何显示相关表中的说明字段

Ruby on rails 3 如何显示相关表中的说明字段,ruby-on-rails-3,activerecord,associations,multiple-tables,Ruby On Rails 3,Activerecord,Associations,Multiple Tables,刚开始玩Ruby(没有IT背景),到现在为止玩得还不错。但两天以来,我被困在这里,不明白出了什么问题。。。非常感谢你帮我解决这个问题!!情况如下: 我创建了一个包含以下列的currencymaster表:currmasdesc:string,currmasiso:string。 我创建了一个包含以下列的currencyrate表:currratemasteridd:integer,currratemasteridc:integer,currraterate:decimal,currratedat

刚开始玩Ruby(没有IT背景),到现在为止玩得还不错。但两天以来,我被困在这里,不明白出了什么问题。。。非常感谢你帮我解决这个问题!!情况如下:

我创建了一个包含以下列的currencymaster表:
currmasdesc:string
currmasiso:string
。 我创建了一个包含以下列的currencyrate表:
currratemasteridd:integer
currratemasteridc:integer
currraterate:decimal
currratedate:date
。其中,
currratemasteridd
列反映主导货币,
currratemasteridc
列反映转换后的货币,以生成组合货币对

models/currencymaster.rb如下所示:

class Currencymaster < ActiveRecord::Base
  has_many :CurrencyRateDom, :class_name => "Currencyrate", :foreign_key => "CurrRateMasterIDD" 
  has_many :CurrencyRateConv, :class_name => "Currencrate", :foreign_key => "CurrRateMasterIDC"
end
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
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
class Currencymaster“Currencyrate”,:foreign\u key=>“currencyMasterIDD”
有很多:CurrencyRateConv,:class\u name=>“currencyrate”,:foreign\u key=>“currencyratemasteridc”
结束
models/currencyrate.rb如下所示:

class Currencymaster < ActiveRecord::Base
  has_many :CurrencyRateDom, :class_name => "Currencyrate", :foreign_key => "CurrRateMasterIDD" 
  has_many :CurrencyRateConv, :class_name => "Currencrate", :foreign_key => "CurrRateMasterIDC"
end
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
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
class Currencyrate'Currencymaster',:foreign\u key=>“CurrencyMasterIDD”,:validate=>true
属于:CurrencyConverted,:class\u name=>Currencymaster',:foreign\u key=>CurrencyMasterIDC',:validate=>true
结束
controllers/currencyrates\u controller.rb如下所示:

class Currencymaster < ActiveRecord::Base
  has_many :CurrencyRateDom, :class_name => "Currencyrate", :foreign_key => "CurrRateMasterIDD" 
  has_many :CurrencyRateConv, :class_name => "Currencrate", :foreign_key => "CurrRateMasterIDC"
end
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
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
class currencyteratescontroller
现在是我的问题,我无法在view/currencyrates/index.html.erb中显示相关的
currencymaster.currmasiso
,而不是存储在表currencyrates中的
currratemasteridd
&
currratemasteridc


我希望在这个问题上所有的信息都是可用的,否则请让我知道什么时候需要其他信息。再次感谢

你为什么不遵守惯例

您应该创建一个
CurrencyMaster
类,不要在列名中重复“currmas”或“currrate”。不要覆盖外键。。。您的代码应该如下所示:

class CurrencyMaster < ActiveRecord::Base
  has_many :currency_rate_doms
  has_many :currency_rate_convs
end
class CurrencyMaster
你的其他课也是这样。Rails使用“约定优先于配置”原则,之后会更好


欢迎来到精彩的ruby/rails世界。

感谢@Dougui的快速回复。我之所以没有遵循约定(正如我现在看到的)是因为我没有很好地理解它们,我想…也许我可以更好地重新启动这个项目…这是互联网上的很多很好的教程。看这个:。(你能站起来认可我的答案吗?)好的,我已经用建议的模型约定和数据库列重新启动了该项目,但仍然想知道如何在currencyrates视图中显示ISO代码。在ruby on rails方法中,我要做什么才能启用此功能?再次感谢!