我对rails有一点问题。我有一个继承的数据库(有一些古怪的列名),看起来有点像这样:
customer
---------
Customer_ID | name | fleet_size | category
1 'bob' 20 60
category
----------
Category_ID | Description
1 'Example Category'因此,一个客户属于一个类别,并且一个类别有许多客户。模型看起来是这样的:
class Category < ActiveRecord::Base
set_table_name "category"
has_many :customers, :foreign_key => "category"
end
class Customer < ActiveRecord::Base
set_table_name "customer"
belongs_to :category, :foreign_key => "Category_ID"
end由于customers表中category列的名称,我认为可能存在冲突,这意味着我不能在视图中调用@customer.category.Description。对解决这个问题有什么想法吗?
发布于 2013-01-15 20:24:33
您可以随时重命名关联:
belongs_to :something_else, :class_name => "Category", :foreign_key => "Category_ID"发布于 2013-01-15 20:25:54
更改关联名称并用作
belongs_to :cust_category, :foreign_key => "Category_ID", :class => 'Category'
@customer.cust_category.Description也许这会解决你的问题。
https://stackoverflow.com/questions/14337573
复制相似问题