我的应用程序中有两个模型,CompanyUk和CompanyName。 CompanyUk hasmany CompanyNames。 Activerecord正在创建CompanyUk.CompanyNames,但它找不到任何CompanyNames。这是代码:
class CompanyName < ActiveRecord::Base
belongs_to :CompanyUk, :foreign_key => "company_number"
end
class CompanyUk < ActiveRecord::Base
has_many :CompanyNames, :dependent => :destroy, :foreign_key => "company_number"
validates_presence_of :company_number, :reg_office, :type, :incorporation_date #, :next_accounts_date, :next_return_date # should eventually have these
validates_uniqueness_of :company_number, :case_sensitive => false
validates_associated :CompanyNames
set_inheritance_column :kind
end
class CreateCompanyUks < ActiveRecord::Migration
def self.up
create_table :company_uks do |t|
t.string :company_number, :null=>false
t.text :reg_office, :null=>false
t.string :type, :null=>false
t.string :business_nature
t.string :status
t.date :incorporation_date, :null=>false
t.date :last_accounts_date
t.date :next_accounts_date
t.date :last_return_date
t.date :next_return_date
t.primary_key :company_number
t.timestamps
end
end
end
class CreateCompanyNames < ActiveRecord::Migration
def self.up
create_table :company_names do |t|
t.string :company_number, :null=>false
t.string :name, :null=>false
t.date :date_adopted, :null=>false
t.timestamps
end
end
end
数据库的内容:
irb(main):011:0> CompanyUk.all
=> [#<CompanyUk id: 1, company_number: "12345", reg_office: "Example office", type: "Private company limited by shares", business_nature: nil, status: nil, incorporation_date: "2011-05-28", last_accounts_date: nil, next_accounts_date: nil, last_return_date: nil, next_return_date: nil, created_at: "2011-05-28 09:47:19", updated_at: "2011-05-28 09:47:19">]
irb(main):012:0> CompanyName.all
=> [#<CompanyName id: 1, company_number: "12345", name: "foo ltd", date_adopted: "2011-05-28", created_at: "2011-05-28 09:47:19", updated_at: "2011-05-28 09:47:19">, #<CompanyName id: 2, company_number: "12345", name: "foo ltd", date_adopted: "2011-05-28", created_at: "2011-05-28 10:09:29", updated_at: "2011-05-28 10:09:29">, #<CompanyName id: 3, company_num
ber: "1", name: "foobar ltd", date_adopted: "2011-05-28", created_at: "2011-05-28 0:10:52", updated_at: "2011-05-28 10:10:52">]
尽管有多个CompanyNames:
irb(main):009:0> CompanyUk.all.first.CompanyNames
=> []
此外,通过CompanyUk.CompanyNames创建新的CompanyName不会正确设置CompanyNumber(CompanyUk的主键和关系上设置的外键)。
那么,有什么方法可以解决这个问题吗?或者更好的参考模型和协会如何运作?
答案 0 :(得分:1)
是否有违反惯例的特殊原因?
例如:这:
has_many :CompanyNames, :dependent => :destroy, :foreign_key => "company_number"
......应该是:
has_many :company_names, :dependent => :destroy
......只要迁移是:
class CreateCompanyNames < ActiveRecord::Migration
def self.up
create_table :company_names do |t|
t.belongs_to :company_uk, :null=>false
t.string :name, :null=>false
t.date :date_adopted, :null=>false
t.timestamps
end
end
end
在Rails 3中使用迁移和关联的指南:
http://guides.rubyonrails.org/migrations.html
http://guides.rubyonrails.org/association_basics.html
请注意,Rails在很大程度上依赖约定优于配置来制作Just Work(TM)。违反这些惯例,除非确实有必要,否则经常会引起头疼:)