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

时间:2011-12-12 21:54:44

标签: ruby-on-rails activerecord rails-3.1

请注意,当我获得产品国家/地区时,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">]

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

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

1 个答案:

答案 0 :(得分:0)

这里有一个奇怪的。我没有太多时间调查原因

attr_accessor :_id

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