2011-12-12 121 views
2

注意,當我獲得產品國家時,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

回答

0

那麼這裏是一個奇怪的一個。沒有太多時間去調查爲什麼,但我不得不

attr_accessor :_id 

在我的國家的模式,因爲我以前從MongoDB中的數據讀取。因爲我不再使用MongoDB,所以刪除了這個問題解決了這個問題。