Rails "magic" joins not working - activerecord not finding linked table entries
I have two models in my app, CompanyUk, and CompanyName. CompanyUk hasmany CompanyNames. Activerecord is creating CompanyUk.CompanyNames, but it won't find any CompanyNames. Here's the code:
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=>fa开发者_开发技巧lse
      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
The contents of the database:
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">]
Despite there being multiple CompanyNames:
irb(main):009:0> CompanyUk.all.first.CompanyNames
=> []
In addition, creating a new CompanyName through CompanyUk.CompanyNames doesn't set the CompanyNumber (the primary key for CompanyUk, and the foreign key set on the relations) correctly.
So, is there any way to fix this? Or even better a good reference for how models and associations work?
Is there a particular reason for going against the convention?
E.g.: this:
has_many :CompanyNames, :dependent => :destroy, :foreign_key => "company_number"
... should be:
has_many :company_names, :dependent => :destroy
... as long as the migration is:
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
Guides for working with migrations and associations in Rails 3:
http://guides.rubyonrails.org/migrations.html
http://guides.rubyonrails.org/association_basics.html
Note that Rails relies heavily on convention over configuration to make things Just Work (TM). Going against these conventions, unless really necessary, can often cause a headache :)
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论