我有这样的数据库架构:
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
答案 0 :(得分: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_TEXTS
和COUNTRY_DESIGNATIONS
。 在纠正之后,您应该能够使用以下代码(仅对具有对某些对象具有深层访问权限的更改代码):
- @manufacturer.models.each do |model|
%tr
%p
...
%td= model.country_designation.des_text.FIELD
= link_to 'Show model', model
这将在制造商的每个模型的循环内显示其des_text
的{{1}}。
但要小心:这只是一个例子,只有每个模型都有国家名称且每个国家/地区的名称都有des_text时才会起作用。
不幸的是,您使用现有的数据库方案,这使得很多事情变得更加困难。我会在这里给你以下建议:
country_designation
并在你认为应该运行的命令中输入。