0

I'm trying to use update_attributes on a record, but it fails and I can't figure out why, I must be missing something obvious as I've used that method plenty of times.

I'm trying to seed data for a model that uses Globalize3 for its name variable.

class City < ActiveRecord::Base
  attr_accessible :name

  translates :name
end

Note that City has no column named name.

In the console I have no issue doing something like city.update_attributes(name: "new name"), but the following code (in seeds.rb) keeps failing with Undefined methodfirstfor nil:NilClass:

localized_cities_attributes = [
  { en: { name: "New York City" }, fr: { name: "New York" } },
  { en: { name: "Montreal" }, fr: { name: "Montréal" } }
]
localized_cities_attributes.each do |city_localized_attributes|
  city = nil

  city_localized_attributes.each do |locale, attributes|
    with_locale(locale) do
      if city
        city.update_attributes(name: attributes[:name])
      elsif (city = City.find_by_name(attributes[:name])).nil?
        city = City.create(attributes)
      end
    end
  end
end

with_locale is defined as such:

def with_locale(new_locale, &block)
  return if block.nil?

  locale_to_restore = I18n.locale
  I18n.locale = new_locale
  block.call
  I18n.locale = locale_to_restore
  nil
end
mbillard
  • 38,386
  • 18
  • 74
  • 98
  • I thought this might be the issue (http://stackoverflow.com/questions/9496816/ruby-on-rails-with-globalize3-find-by-translated-field-makes-record-readonly?rq=1), but my record appears to be writable. – mbillard Sep 25 '12 at 14:39
  • 1
    more of the stack trace would help. where is `first` being called? is it in your code? by `Globalize3`? in rails? – gregates Sep 25 '12 at 14:43
  • Agreed with @gregates. The error you're showing us has to do with the use of a call to the 'first' method on a nil object. I don't see a call to 'first' in your example. Full stack trace, more code, or both please? – coderjoe Sep 25 '12 at 14:54
  • I use this code in `seeds.rb`, so by using `rake db:seed`. I only get the following messages: `rake aborted! undefined method `first' for nil:NilClass` – mbillard Sep 25 '12 at 14:55
  • @gregates thanks, can't believe I didn't think of that. Here's the gist: https://gist.github.com/a65ae965d5846bdb8f6c – mbillard Sep 25 '12 at 15:01

1 Answers1

1

I finally figured it out using the trace.

It turns out my custom method with_locale is also defined by globalize3 and caused all sorts of issues.

Thanks to @cthulhu, I found out about I18n.with_locale and used that instead.

mbillard
  • 38,386
  • 18
  • 74
  • 98
  • 1
    cool, I was just about to post that! glad we could (sort of) help – gregates Sep 25 '12 at 15:07
  • 1
    BTW there is a method I18n.with_locale that does the same thing that your method does, so why not use it ? – cthulhu Sep 25 '12 at 15:09
  • @gregates you sure helped, I wouldn't have thought about using `trace` without you, I have no idea why since I usually do that. @cthulhu I was not aware of that method, I will use it and get rid of mine. – mbillard Sep 25 '12 at 15:34