Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Unable to fetch belongs_to association in loop #307

Open
FergusonSean opened this issue Jun 30, 2017 · 0 comments
Open

Unable to fetch belongs_to association in loop #307

FergusonSean opened this issue Jun 30, 2017 · 0 comments

Comments

@FergusonSean
Copy link

When running the Package.all_for_account_by_delivery_date method here I get a strange error. It seems that when I am in the Package#as_json method the package cannot access it's customer or address. Any idea why this is?

Here is the error:

TypeError - compared with non class/module:
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/collection.rb:1467:in `>'
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/collection.rb:1467:in `block in delegate_to_model'
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/model/scope.rb:53:in `block in with_scope'
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/model/scope.rb:73:in `with_exclusive_scope'
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/model/scope.rb:53:in `with_scope'
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/collection.rb:1466:in `delegate_to_model'
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/collection.rb:1445:in `method_missing'
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/associations/many_to_one.rb:186:in `lazy_load'
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/resource/persistence_state/persisted.rb:23:in `lazy_load'
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/resource/persistence_state/persisted.rb:8:in `get'
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/model/relationship.rb:340:in `customer'
	/app/lib/package.rb:26:in `as_json'
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/collection.rb:508:in `block in each'
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/support/lazy_array.rb:411:in `block in each'
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/support/lazy_array.rb:411:in `each'
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/support/lazy_array.rb:411:in `each'
	/usr/local/bundle/gems/dm-core-1.2.1/lib/dm-core/collection.rb:505:in `each'
	/app/lib/package.rb:20:in `map'
	/app/lib/package.rb:20:in `all_for_account_by_delivery_date'
	server.rb:52:in `block in <class:Server>'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1632:in `call'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1632:in `block in compile!'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:991:in `block (3 levels) in route!'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1010:in `route_eval'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:991:in `block (2 levels) in route!'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1037:in `block in process_route'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1035:in `catch'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1035:in `process_route'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:989:in `block in route!'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:988:in `each'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:988:in `route!'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1094:in `block in dispatch!'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1073:in `block in invoke'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1073:in `catch'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1073:in `invoke'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1091:in `dispatch!'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:923:in `block in call!'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1073:in `block in invoke'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1073:in `catch'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1073:in `invoke'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:923:in `call!'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:913:in `call'
	/usr/local/bundle/gems/rack-protection-2.0.0/lib/rack/protection/xss_header.rb:18:in `call'
	/usr/local/bundle/gems/rack-protection-2.0.0/lib/rack/protection/path_traversal.rb:16:in `call'
	/usr/local/bundle/gems/rack-protection-2.0.0/lib/rack/protection/json_csrf.rb:26:in `call'
	/usr/local/bundle/gems/rack-protection-2.0.0/lib/rack/protection/base.rb:50:in `call'
	/usr/local/bundle/gems/rack-protection-2.0.0/lib/rack/protection/base.rb:50:in `call'
	/usr/local/bundle/gems/rack-protection-2.0.0/lib/rack/protection/frame_options.rb:31:in `call'
	/usr/local/bundle/gems/rack-2.0.3/lib/rack/logger.rb:15:in `call'
	/usr/local/bundle/gems/rack-2.0.3/lib/rack/common_logger.rb:33:in `call'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:231:in `call'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:224:in `call'
	/usr/local/bundle/gems/rack-2.0.3/lib/rack/head.rb:12:in `call'
	/usr/local/bundle/gems/rack-2.0.3/lib/rack/method_override.rb:22:in `call'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/show_exceptions.rb:22:in `call'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:194:in `call'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1955:in `call'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1499:in `block in call'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1726:in `synchronize'
	/usr/local/bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:1499:in `call'
	/usr/local/bundle/gems/thin-1.7.1/lib/thin/connection.rb:86:in `block in pre_process'
	/usr/local/bundle/gems/thin-1.7.1/lib/thin/connection.rb:84:in `catch'
	/usr/local/bundle/gems/thin-1.7.1/lib/thin/connection.rb:84:in `pre_process'
	/usr/local/bundle/gems/thin-1.7.1/lib/thin/connection.rb:50:in `block in process'
	/usr/local/bundle/gems/eventmachine-1.2.3/lib/eventmachine.rb:1076:in `block in spawn_threadpool'

And here is the relevant code:

class Account
  include DataMapper::Resource

  has n, :packages
  has n, :recipes
  has n, :sizes

  property :id, Integer, key: true
  property :name, String
end

class Address
  include DataMapper::Resource

  belongs_to :account
  has n, :packages

  property :id, Serial
  property :first_name, String
  property :address1, String
  property :phone, String
  property :city, String
  property :zip, String
  property :province, String
  property :country, String
  property :last_name, String
  property :address2, String
  property :company, String
  property :latitude, Decimal, scale: 12, precision: 15
  property :longitude, Decimal, scale: 12, precision: 15
  property :name, String
  property :country_code, String
  property :province_code, String

end

class Customer
  include DataMapper::Resource

  belongs_to :account
  belongs_to :address
  has n, :packages

  property :id, Serial
  property :external_id, String, allow_nil: false
  property :first_name, String, allow_nil: false
  property :last_name, String, allow_nil: false
  property :email, String, allow_nil: false

  def name
    "#{first_name} #{last_name}"
  end
end

class MealAssignment
  include DataMapper::Resource

  belongs_to :recipe
  belongs_to :package

  property :id, Serial
  property :quantity, Integer
end

class Recipe
  include DataMapper::Resource

  belongs_to :account
  property :id, Serial
  property :name, String
  property :description, Text

end

class Size
  include DataMapper::Resource

  belongs_to :account
  property :id, Serial
  property :name, String
  property :amount, Decimal, scale: 2, precision: 6 

end

class Package
  include DataMapper::Resource

  has n, :meal_assignments
  belongs_to :customer
  belongs_to :address
  belongs_to :account
  belongs_to :size, required: false

  property :id, Serial
  property :deliver_on, Date
  property :pack, String, length: 255
  property :required_meals, Integer
  property :special_instructions, Text
  property :external_order_id, String

  def self.all_for_account_by_delivery_date(account)
    account.packages.map(&:as_json).group_by{|e| e[:deliver_on]}
  end

  def as_json(options = {})
    {
      deliver_on: deliver_on,
      customer_name: customer.name,
      address: address.to_s,
      size: size.as_json,
      pack: pack,
      required_meals: required_meals,
      special_instructions: special_instructions,
      external_order_id: external_order_id,
      meals: meal_assignments.map{|ma| ma.recipe.as_json}
    }
  end

  def self.get_delivery_dates_for_account(account)
    repository(:default).adapter.select(<<-SQL, account.id
      SELECT distinct deliver_on 
      FROM packages 
      WHERE account_id = ?
      ORDER BY deliver_on DESC
      SQL
    )
  end
end
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant