0

我有这样的数据库架构:

Tables:
COUNTRY_DESIGNATIONS
DES_TEXTS
MANUFACTURERS
MODELS

以及这样的关系:

COUNTRY_DESIGNATIONS has_many MODELS
DES_TEXTS has_many COUNTRY_DESIGNATIONS
MANUFACTURERS has_many MODELS

在 Rails 模型中,所有关系和其他“事物”都被写入。

当我选择制造商时,我会得到它的所有型号。但是现在我想在这些模型中从 COUNTRY_DESIGNATIONS 中选择数据(确保所有表关系 id 必须相等),然后当从 COUNTRY_DESIGNATIONS 中选择数据时,我想从 DES_TEXTS 中获取数据并显示它。

我怎样才能做到这一点?控制器、视图有什么变化?(我使用的是标准脚手架)

现在我有这样的观点来查看制造商的模型:

  - @manufacturer.models.each do |model|
    %tr
      %p
        mod_id
        %td= model.MOD_ID
      %p
        MOD_PCON_START
        %td= model.MOD_PCON_START
      %p
        MOD_PCON_END
        %td= model.MOD_PCON_END
      = link_to 'Show model', model

我想添加这样的东西:

  - @manufacturer.models.each do |model|
    %tr
      %p
        ...
        %td= model.country_des.des_text.FIELD  - something like this)
      = link_to 'Show model', model

模型文件:

class CountryDesignation < ActiveRecord::Base
    set_table_name "COUNTRY_DESIGNATIONS"
    set_primary_key :CDS_ID
    belongs_to :des_text
    belongs_to :language
    has_many :models
end

class DesText < ActiveRecord::Base
    set_table_name "DES_TEXTS"
    set_primary_key :TEX_ID
    has_many :country_designation
    has_many :designation
end

class Model < ActiveRecord::Base
    set_table_name "MODELS"
    set_primary_key :MOD_ID
    belongs_to :manufacturer
    belongs_to :country_designation
    has_many :types
end



dump:
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120418164608) do

  create_table "COUNTRY_DESIGNATIONS", :primary_key => "CDS_ID", :force => true do |t|
    t.binary   "CDS_CTM"
    t.integer  "CDS_LNG_ID"
    t.integer  "CDS_TEX_ID"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "DESIGNATIONS", :primary_key => "DES_ID", :force => true do |t|
    t.integer  "DES_LNG_ID"
    t.integer  "DES_TEX_ID"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "DES_TEXTS", :primary_key => "TEX_ID", :force => true do |t|
    t.text     "TEX_TEXT"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "LANGUAGES", :primary_key => "LNG_ID", :force => true do |t|
    t.integer  "LNG_DES_ID"
    t.string   "LNG_ISO2"
    t.string   "LNG_CODEPAGE"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "MANUFACTURERS", :primary_key => "MFA_ID", :force => true do |t|
    t.integer  "MFA_PC_MFC"
    t.integer  "MFA_CV_MFC"
    t.integer  "MFA_AXL_MFC"
    t.integer  "MFA_ENG_MFC"
    t.integer  "MFA_ENG_TYP"
    t.string   "MFA_MFC_CODE"
    t.string   "MFA_BRAND"
    t.integer  "MFA_MF_NR"
    t.binary   "MFA_PC_CTM"
    t.binary   "MFA_CV_CTM"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "MODELS", :primary_key => "MOD_ID", :force => true do |t|
    t.integer  "MOD_MFA_ID"
    t.integer  "MOD_CDS_ID"
    t.integer  "MOD_PCON_START"
    t.integer  "MOD_PCON_END"
    t.integer  "MOD_PC"
    t.integer  "MOD_CV"
    t.integer  "MOD_AXL"
    t.binary   "MOD_PC_CTM"
    t.binary   "MOD_CV_CTM"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "TYPES", :primary_key => "TYP_ID", :force => true do |t|
    t.integer  "TYP_CDS_ID"
    t.integer  "TYP_MMT_CDS_ID"
    t.integer  "TYP_MOD_ID"
    t.binary   "TYP_CTM"
    t.binary   "TYP_LA_CTM"
    t.integer  "TYP_SORT"
    t.integer  "TYP_PCON_START"
    t.integer  "TYP_PCON_END"
    t.integer  "TYP_KW_FROM"
    t.integer  "TYP_KW_UPTO"
    t.integer  "TYP_HP_FROM"
    t.integer  "TYP_HP_UPTO"
    t.integer  "TYP_CCM"
    t.integer  "TYP_CYLINDERS"
    t.integer  "TYP_DOORS"
    t.integer  "TYP_TANK"
    t.integer  "TYP_KV_VOLTAGE_DES_ID"
    t.integer  "TYP_KV_ABS_DES_ID"
    t.integer  "TYP_KV_ASR_DES_ID"
    t.integer  "TYP_KV_ENGINE_DES_ID"
    t.integer  "TYP_KV_BRAKE_TYPE_DES_ID"
    t.integer  "TYP_KV_BRAKE_SYST_DES_ID"
    t.integer  "TYP_KV_FUEL_DES_ID"
    t.integer  "TYP_KV_CATALYST_DES_ID"
    t.integer  "TYP_KV_BODY_DES_ID"
    t.integer  "TYP_KV_STEERING_DES_ID"
    t.integer  "TYP_KV_STEERING_SIDE_DES_ID"
    t.float    "TYP_MAX_WEIGHT"
    t.integer  "TYP_KV_MODEL_DES_ID"
    t.integer  "TYP_KV_AXLE_DES_ID"
    t.integer  "TYP_CCM_TAX"
    t.float    "TYP_LITRES"
    t.integer  "TYP_KV_DRIVE_DES_ID"
    t.integer  "TYP_KV_TRANS_DES_ID"
    t.integer  "TYP_KV_FUEL_SUPPLY_DES_ID"
    t.integer  "TYP_VALVES"
    t.integer  "TYP_RT_EXISTS"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "carts", :force => true do |t|
    t.integer  "customer_id"
    t.integer  "item_id"
    t.integer  "amount"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "credit_cards", :force => true do |t|
    t.integer  "customer_id"
    t.string   "number"
    t.string   "nameOfCard"
    t.date     "expiryDate"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "currencies", :force => true do |t|
    t.float    "currencyvalue"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "customer_sessions", :force => true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "customers", :force => true do |t|
    t.string   "username"
    t.string   "crypted_password"
    t.string   "password_salt"
    t.string   "persistence_token"
    t.string   "email"
    t.string   "skype"
    t.integer  "ICQ"
    t.string   "firstname"
    t.string   "lastname"
    t.string   "country"
    t.string   "state"
    t.string   "city"
    t.string   "street"
    t.string   "building"
    t.integer  "room"
    t.string   "addressNote"
    t.string   "dateOfReg"
    t.integer  "custGroup_id"
    t.float    "totalBuy"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "order_statuses", :force => true do |t|
    t.string   "statusname"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "orders", :force => true do |t|
    t.integer  "basket_id"
    t.integer  "customer_id"
    t.integer  "shipping_id"
    t.integer  "paymentmethod_id"
    t.integer  "orderstatus_id"
    t.datetime "dateoforder"
    t.float    "totalcost"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "payment_methods", :force => true do |t|
    t.string   "methodname"
    t.boolean  "allowed"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "sellers", :force => true do |t|
    t.string   "username"
    t.string   "crypted_password"
    t.string   "password_salt"
    t.string   "persistence_token"
    t.string   "email"
    t.string   "skype"
    t.integer  "ICQ"
    t.string   "firstname"
    t.string   "lastname"
    t.string   "dateOfReg"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "shippings", :force => true do |t|
    t.string   "shippingname"
    t.float    "shippingcost"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "telephone_operators", :force => true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "telephones", :force => true do |t|
    t.integer  "customer_id"
    t.integer  "operator"
    t.integer  "number"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "virtual_pay_systems", :force => true do |t|
    t.string   "name"
    t.boolean  "active"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "virtual_pays", :force => true do |t|
    t.integer  "customer_id"
    t.string   "number"
    t.integer  "virtualpaysystem_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end
4

1 回答 1

0

您编写的代码应该大部分都可以工作,但是其中有一个细微的错误:

  • belongs_to关联使用单数(如belongs_to :manufacturer),但对于关联has_many,您必须使用复数:

    class DesText < ActiveRecord::Base
        set_table_name "DES_TEXTS"
        set_primary_key :TEX_ID
        has_many :country_designations
        has_many :designations
    end
    
  • 请确保在您的表格中设置了相关键。以下是适合您的规则:
    • 每个has_many关联(参见Rails Guides)都需要 many 部分具有对 one 部分的引用。在您的示例中:Model需要引用CountryDesignation. Rails 认为您的模型表包含一列country_designation_id(它不会找到)。
    • DES_TEXTS和类似COUNTRY_DESIGNATIONS

更正后,您应该能够使用以下代码(仅更改后的代码可以对某些对象进行深度访问):

- @manufacturer.models.each do |model|
  %tr
    %p
      ...
      %td= model.country_designation.des_text.FIELD
    = link_to 'Show model', model

这将在每个制造商型号的循环内显示des_textcountry_designation. 但要小心:这只是一个示例,并且只有在每个模型都有一个国家名称并且每个国家名称都有一个 des_text 的情况下才会起作用。

不幸的是,您使用现有的数据库方案,它使很多事情变得更加困难。我在这里给你以下建议:

  • 迁移到名称为 rails 嫌疑人的干净状态。
  • 每次迁移后使用 Rails 控制台测试您的模型是否符合规则。您可以从那里开始rails c并在那里输入您认为应该有效的命令。
  • 您的应用程序是一个庞大而复杂的时刻,所以我认为没有机会在这里进一步调试它......(对不起,但这是我第四次添加到我的答案中,它看起来更像是Rails 教程现在)
于 2012-04-21T10:02:40.380 回答