2012-04-20 21 views
0

我有這樣的DB模式:如何在rails應用程序中顯示困難模型視圖?

Tables: 
COUNTRY_DESIGNATIONS 
DES_TEXTS 
MANUFACTURERS 
MODELS 

而這樣的關係:

COUNTRY_DESIGNATIONS has_many MODELS 
DES_TEXTS has_many COUNTRY_DESIGNATIONS 
MANUFACTURERS has_many MODELS 

在鐵軌模型中的所有關係和其他 「東西」 被寫入。

當我選擇製造商,我得到它的所有模型。但現在我希望在這些模型中從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 
+0

你可以添加你的模型而不是「關係」嗎?如果你的模型包含普通的東西(比如'class Model; belongs_to country_designation ... ...),你通常應該能夠從'Model'導航到'CountryDesignation'到'DesText'。 – mliebelt 2012-04-20 20:43:18

+0

@mliebelt我添加了代碼。請幫幫我。 – byCoder 2012-04-21 08:58:09

+0

如果我的答案對您不適用,可能是您的遷移不適合我們編寫的模型代碼。然後你應該添加模式轉儲(可以在'db/schema.rb'中找到)。 – mliebelt 2012-04-21 10:43:04

回答

0

你寫的應該基本能工作,但在這一個微妙的錯誤代碼:

  • 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 c開始,然後在你認爲應該工作的命令中輸入。
  • 你的應用程序是一個大而複雜的時刻,所以我不認爲有機會在這裏進一步調試......(對不起,但這是我第四次添加到我的答案,它看起來像更像現在的Rails教程)
+0

好的,謝謝,但我問的是如何在視圖中使用它?如何在視圖中顯示(製造商 - >型號,然後從模型中選擇國家,然後選擇des_text)? – byCoder 2012-04-21 10:30:02

+0

未定義的方法''爲零:NilClass – byCoder 2012-04-21 13:12:59

+0

未定義的方法錯誤 – byCoder 2012-04-21 13:29:28

相關問題