【问题标题】:How to display difficult-model view in rails app?如何在 Rails 应用程序中显示困难模型视图?
【发布时间】:2012-04-20 20:06:19
【问题描述】:

我有这样的数据库架构:

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

【问题讨论】:

  • 你能添加你的模型而不是“关系”吗?如果你的模型包含正常的东西(比如class Model; belongs_to country_designation...),你通常应该能够从Model导航到CountryDesignationDesText
  • @mliebelt 我添加了代码。请帮帮我。
  • 如果我的回答对您不起作用,可能是您的迁移不适合我们编写的模型代码。然后,您还应该添加架构转储(在 db/schema.rb 中找到)。

标签: ruby-on-rails model-view-controller view


【解决方案1】:

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

  • belongs_to association 使用单数(如belongs_to :manufacturer),但对于associationhas_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)都需要多部分具有对一个部分的引用。在您的示例中:Model 需要引用 CountryDesignation。 Rails 认为您的模型表包含 country_designation_id 列(它不会找到)。
    • DES_TEXTSCOUNTRY_DESIGNATIONS 类似。

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

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

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

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

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

【讨论】:

  • 好的,谢谢,但我问的是如何在视图中使用它?如何在视图中显示(制造商->模型,然后从国家/地区选择模型,然后选择des_text)?
  • nil:NilClass 的未定义方法``
  • 注意!这是为制造商展示视图和控制器完成的
  • 在这个数据库中,all has all )) 但我仍然遇到错误,不知道如何解决它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 2013-05-15
  • 2017-09-14
  • 1970-01-01
相关资源
最近更新 更多