2

请注意,当我获得产品国家/地区时,ID 为零。我在 Rails 3.1 上。有谁知道为什么会这样?

>> 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">]

我注意到的另一件奇怪的事情是,如果我循环并检查它,主键实际上会打印出来。因此,如果我在 Product 模型中并执行以下操作:

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

4

1 回答 1

0

好吧,这是一个奇怪的。没有太多时间调查原因,但我有

attr_accessor :_id

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

于 2011-12-13T21:41:45.180 回答