我有两个模型:
class Subscription
belongs_to :subscriber, :class_name => "User"
belongs_to :subscribable, :polymorphic => true
end
class Product
belongs_to :user
has_many :subscriptions, :as => :subscribable, :dependent => :destroy
end
create_table :products do |t|
t.string :name
t.decimal :price
t.decimal :cost_per_unit
t.integer :user_id
end
create_table :subscriptions do |t|
t.string :name
t.decimal :price
t.decimal :cost_per_unit
t.integer :subscriber_id
t.integer :subscribable_id
t.string :subscribable_type
end
在我的产品模型中,我试图制作一个将price
orcost_per_unit
与. 我只是不知道如何用 SQL 编写它。所以它应该是这样的:Product
Subscription
class Product
def self.lower_prices
where( self.price < self.subscription.price OR self.cost_per_unit < self.subscription.cost_per_unit )
end
end
我怎么写这个?