Use with Mongoid - undefined local variable or method `acts_as_gmappable' #162

Closed
kristianmandrup opened this Issue Mar 15, 2012 · 6 comments

2 participants

@kristianmandrup

Hi, I just tried to follow the example from the README and illustrated in the screencast. I have a basic Mongoid setup with a model Property that has an address embedded:

class Property
  include Mongoid::Document

  acts_as_gmappable

  field :title, :type => String
  field :desc,  :type => String

  embeds_one :address

    #describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki 
    def gmaps4rails_address
    "#{address.street}, #{address.city}, #{address.country}" 
    end
end
class Address
  include Mongoid::Document

  field :latitude,  :type => Float

  field :street,    :type => String
  field :city,      :type => String
  field :zipcode,   :type => Integer
  field :country,   :type => String

    embedded_in :property
end

seed.rb

address1 = Address.create {
    street: 'maglekildevej 18', city: 'Frederiksberg', country: 'Denmark', zipcode: 2000, 
    latitude: 55.675557, longitude: 12.535189
}

address2 = Address.create {
    street: 'lergravsvej 19', city: 'Amager', country: 'Denmark', zipcode: 2300,
    latitude: 55.66289, longitude: 12.618063
}

addresses = [address1, address2]

properties = Property.create(
    [{ title: 'Frb delight', address: addresses.first }, { name: 'Amager wonder', address: addresses.last }]
)
$ rake db:seed
rake aborted!
undefined local variable or method `acts_as_gmappable' for Property:Class

Tasks: TOP => db:seed => environment
(See full trace by running task with --trace)
@apneadiving
Owner

Yep,

Seems I can't manage to include the module properly in Mongoid. Makes me crazy.

Well, simply include the module manually then:

include Gmaps4rails::ActsAsGmappable
acts_as_gmappable
@kristianmandrup

Yeah, it's a problem with Mongoid. You could override the inherit hook, but then you run into other troubles with other gems.
Mongoid really should use a subscriber pattern on the inherit hook...thanks!

@kristianmandrup

OK, I tried this:

class Property
  include Mongoid::Document
  include Gmaps4rails::ActsAsGmappable

  acts_as_gmappable

  field :title, :type => String
  field :desc,  :type => String

  embeds_one :address

  #describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki   
  def gmaps4rails_address
    "#{address.street}, #{address.city}, #{address.country}" 
  end
end
address1 = Address.new street: 'maglekildevej 18', city: 'Frederiksberg', zipcode: 2000, country: 'Denmark',  latitude: 55.675557, longitude: 12.535189

address2 = Address.new street: 'lergravsvej 19', city: 'Amager', zipcode: 2300, country: 'Denmark', latitude: 55.66289, longitude: 12.618063

addresses = [address1, address2]

property1 = Property.create title: 'Frb delight', address: addresses.first <--- line 13
undefined method `gmaps' for #
/Users/kmandrup/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.4.6/lib/mongoid/attributes.rb:166:in `method_missing'
/Users/kmandrup/.rvm/gems/ruby-1.9.2-p290/gems/gmaps4rails-1.4.7/lib/gmaps4rails/acts_as_gmappable.rb:15:in `process_geocoding'
/Users/kmandrup/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:407:in `_run__227527150166666
...
/Users/kmandrup/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.4.6/lib/mongoid/persistence.rb:49:in `insert'
/Users/kmandrup/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.4.6/lib/mongoid/persistence.rb:154:in `upsert'
/Users/kmandrup/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.4.6/lib/mongoid/persistence.rb:177:in `block (2 levels) in create'
/Users/kmandrup/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.4.6/lib/mongoid/persistence.rb:177:in `tap'
/Users/kmandrup/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.4.6/lib/mongoid/persistence.rb:177:in `block in create'
/Users/kmandrup/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.4.6/lib/mongoid/threaded/lifecycle.rb:173:in `_creating'
/Users/kmandrup/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.4.6/lib/mongoid/persistence.rb:176:in `create'
/Users/kmandrup/private/repos/real_estate/db/seeds.rb:13:in `'
@kristianmandrup

line 13 in seeds.rb is: property1 = ...

@apneadiving
Owner

Ok this one is logic: as per Readme, you need to add a Booelan field called gmaps to your model.

This can be skipped, see wiki, but I recommend you keep it so that geocoding is already made once.

Here is the relevant part:

add_column :users, :latitude, :float #you can change the name, see wiki
add_column :users, :longitude, :float #you can change the name, see wiki
add_column :users, :gmaps, :boolean #not mandatory, see wiki
@kristianmandrup

Thanks :) I got it working now.

class Property
  include Mongoid::Document
    include Gmaps4rails::ActsAsGmappable

  acts_as_gmappable

  field :gmaps, :type => Boolean

  field :title, :type => String
  field :desc,  :type => String

  embeds_one :address

  delegate :latitude, :longitude, :to => :address

    #describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki 
    def gmaps4rails_address
    "#{address.street}, #{address.city}, #{address.country}" 
    end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment