0

I have the following schema:

class Locale < ActiveRecord::Base
  has_many :shops

  # region : String
end

class Shop < ActiveRecord::Base
  belongs_to :locale
  has_many :carts

  scope :europe, joins(:locale).where('locales.region = ?', 'Europe')
end

class Cart < ActiveRecord::Base
  belongs_to :shop

  scope :purchased, where('purchased_at is not null')

  # purchased_at : DateTime
end

I want to find all carts that have been purchased in a certain region I have a couple of scopes setup to make the query a little more readable but when I try:

Cart.purchased.join(:shop).merge(Shop.europe)

I get the error: ActiveRecord::ConfigurationError: Association named 'locale' was not found; perhaps you misspelled it?

Any thoughts on how I can make this work?

KJF
  • 2,083
  • 4
  • 21
  • 38
  • the table name in your where is 'locales', but the class is 'locale', or is it some ruby magic adding the s? – Puggan Se Jul 04 '12 at 14:03

1 Answers1

0
class Locale < ActiveRecord::Base
  has_many :shops

  scope :europe, where(region: 'Europe')
end

class Shop < ActiveRecord::Base
  belongs_to :locale
  has_many :carts
end


class Cart < ActiveRecord::Base
  belongs_to :shop

  scope :purchased, where('purchased_at is not null')
end

Cart.purchased.joins(shop: :locale).merge(Locale.europe)
Subba Rao
  • 10,576
  • 7
  • 29
  • 31