0

我有以下架构:

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

我想查找在某个地区购买的所有购物车我设置了几个范围以使查询更具可读性但是当我尝试时:

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

我收到错误:ActiveRecord::ConfigurationError: Association named 'locale' is not found; 也许你拼错了?

关于如何完成这项工作的任何想法?

4

1 回答 1

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)
于 2012-07-06T20:15:15.110 回答