没有在Rails中获取关联对象的主键

问题描述:

注意,当我获得产品国家时,ID为零。我在Rails 3.1上。任何人都知道这是为什么发生?没有在Rails中获取关联对象的主键

>> p = Product.first 
    Product Load (1.3ms) SELECT `products`.* FROM `products` LIMIT 1 
=> #<Product id: 1549, context_date: "2011-10-01 00:00:00", expiration_date: "2013-04-13 16:13:57", blurb: "do you like '", created_at: "2011-10-13 16:13:57", updated_at: "2011-10-13 16:13:57", product_type_id: 31, approved_at: nil, state: "waiting", archived_at: nil, name: "some product name", custom_name: "Product with '", secret: "secret", ups_owner_id: "4d911fc87b074638be000008", avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil, is_global: false, ups_author_id: "4d911fc87b074638be000017", parent_id: nil, product_child_type_id: nil, finished_at: nil> 
>> p.countries 
    Country Load (0.9ms) SELECT `countries`.* FROM `countries` INNER JOIN `product_countries` ON `countries`.`id` = `product_countries`.`country_id` WHERE `product_countries`.`product_id` = 1549 
=> [#<Country id: nil, name: "Argentina", iso_two_letter_code: "AR", created_at: "2010-09-24 19:02:43", updated_at: "2010-09-24 19:02:43">, #<Country id: nil, name: "Brazil", iso_two_letter_code: "BR", created_at: "2010-09-24 19:02:43", updated_at: "2010-09-24 19:02:43">, #<Country id: nil, name: "Chile", iso_two_letter_code: "CL", created_at: "2010-09-24 19:02:43", updated_at: "2010-09-24 19:02:43">, #<Country id: nil, name: "Colombia", iso_two_letter_code: "CO", created_at: "2010-09-24 19:02:43", updated_at: "2010-09-24 19:02:43">, #<Country id: nil, name: "Costa Rica", iso_two_letter_code: "CR", created_at: "2010-09-24 19:02:43", updated_at: "2010-09-24 19:02:43">, #<Country id: nil, name: "Dominican Republic", iso_two_letter_code: "DO", created_at: "2010-09-24 19:02:43", updated_at: "2010-09-24 19:02:43">, #<Country id: nil, name: "Mexico", iso_two_letter_code: "MX", created_at: "2010-09-24 19:02:43", updated_at: "2010-09-24 19:02:43">, #<Country id: nil, name: "Paraguay", iso_two_letter_code: "PY", created_at: "2010-09-24 19:02:43", updated_at: "2010-09-24 19:02:43">, #<Country id: nil, name: "Peru", iso_two_letter_code: "PE", created_at: "2010-09-24 19:02:43", updated_at: "2010-09-24 19:02:43">, #<Country id: nil, name: "Venezuela", iso_two_letter_code: "VE", created_at: "2010-09-24 19:02:43", updated_at: "2010-09-24 19:02:43">] 

我注意到的另一个奇怪的事情是,如果我循环并检查它,主键将实际打印出来。所以,如果我在产品型号和做:

self.countries.each do |c| 
    logger.info "c.id = #{c.id}" 
end 

我其实得到的主键。然而,我最初注意到这一点,因为当试图抓住它的关联时,国家的主键似乎是零,因此总是没有返回。例如:

 self.countries.each do |c| 
     logger.info "c.id = #{c.id}" 
     logger.info "c.country_groups = #{c.country_groups.inspect}" 
     end 

生成此在Rails日志:

c.id = 101 
    CountryGroup Load (0.6ms) SELECT `country_groups`.* FROM `country_groups` INNER JOIN `countries_country_groups` ON `country_groups`.`id` = `countries_country_groups`.`country_group_id` WHERE `countries_country_groups`.`country_id` IS NULL 
c.country_groups = [] 

所以我还是很困惑,为什么它检查country_id IS NULL

那么这里是一个奇怪的一个。没有太多时间去调查为什么,但我不得不

attr_accessor :_id 

在我的国家的模式,因为我以前从MongoDB中的数据读取。因为我不再使用MongoDB,所以删除了这个问题解决了这个问题。