سؤال

Notice the ids are nil when I get product countries. I'm on Rails 3.1. Anyone know why this is happening?

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

Another odd thing I notice is that the primary key will actually print out if I loop and check it. So if I am in the Product model and do:

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

I do in fact get the primary keys. However, I noticed this originally because the country primary key seems to be nil when trying to grab it's associations and therefore always returns nothing. For example:

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

Generates this in the Rails log:

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 = []

So I'm still pretty confused about why it's checking for country_id IS NULL

هل كانت مفيدة؟

المحلول

Well here's a weird one. Not much time to investigate why but I had

attr_accessor :_id

In my country model since I used to read in that data from MongoDB. Removing that fixed the issue since I'm no longer using MongoDB.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top