ruby-on-rails - 创建一种方法或范围来显示较低的价格?

标签 ruby-on-rails ruby ruby-on-rails-3

我对如何制定这个范围或方法感到困惑。我有以下关联:

型号

class User
  has_many :prices
  has_many :products, :through => :prices
  has_many :subscriptions, :foreign_key => :subscriber_id
end

class Product
  has_many :prices
  has_many :users, :through => :prices
end

class Price
  # Table columns => :product_id, :cost, :user_id
  belongs_to :user
  belongs_to :product
  belongs_to :store
  has_many :subscriptions, :as => :subscribable
end

class Subscription
  # Table columns => :product_id, :cost, :subscriber_id, :subscribable_id
  # :subscribable_type
  belongs_to :subscriber, :class_name => "User"
  belongs_to :subscribable, :polymorphic => true
  validates_uniqueness_of :subscribable_id, :scope => 
                        [ :subscriber_id, :subscribable_type]
end

所以方法应该是这样的:

class Price

def self.lower_price
  if self.product_id == self.subscription.product_id
     if self.cost < self.subscription.cost
     end
  end
end

end

此方法的作用是仅显示与 Subscription 属于同一 ProductUserProducts 的较低价格,同时进行比较本身到订阅 price 字段,看看它是否更低。

我这样做对吗?需要修复什么?


编辑

class Price < ActiveRecord::Base
  scope :for_product, lambda { |product_id| where( :product_id => product_id) }
  scope :cheaper, lambda { |cost| where(["prices.cost < :cost", { :cost => cost } ] ) }
end

class Subscription < ActiveRecord::Base

  def cheaper_prices
    Price.for_product(product_id).cheaper(cost)
  end
end

PrivatePagesController:

def watch
 @prices = Price.cheaper_prices.paginate(:page => params[:page], :per_page => 20).order('purchase_date DESC')
end

This gives me the error:

NoMethodError in PrivatePagesController#watch

undefined method `cheaper_prices' for #<Class:0x6f99210>

最佳答案

我认为您正在制作一个网站,用户可以在其中输入他们找到的价格并订阅以找到更便宜的价格。我将 UserProduct 实体重命名为 Price。

订阅者订阅的是产品还是价格是不明确的。如果你弄清楚了这一点,它可能会简化多态关联。假设他们以给定价格订阅产品。那么您需要以下内容:

class Price
  # Table columns => :product_id, :price, :user_id
  belongs_to :finder, :class_name => "User"
  belongs_to :product
  belongs_to :store

  scope for_product, lambda { |product_id| where(:product_id => product_id)
  scope cheaper, lambda { |price| where([ "prices.price < :price", {:price => price} ] }
end

class Subscription
  # Table columns => :product_id, :price, :subscriber_id
  belongs_to :subscriber, :class_name => "User"
  belongs_to :product
  validates_uniqueness_of :subscribable_id, :scope => 
                        [ :subscriber_id, :subscribable_type]

  def cheaper
    Price.for_product(product_id).cheaper(price)
  end
end

关于ruby-on-rails - 创建一种方法或范围来显示较低的价格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10099529/

相关文章:

ruby-on-rails - 如何从 rails3 Controller 中的复选框获取参数

ruby-on-rails - Vim E854 : path too long for completion

ruby-on-rails - Rails - 验证记录而不添加错误

ruby - 使 headless 浏览器停止加载页面

mysql - Rails 3.2 - 无法连接到本地 MySQL

ruby-on-rails - Spree 仅在不同页面的侧边栏上显示选定的分类单元

ruby-on-rails - 在 IRB 中使用 Yahoo Fantasy Sports API

Javascript、Rails View 、content_for 和 DRYness

ruby - 如何使用 sinatra session

ruby-on-rails - 嵌套注册表单不显示嵌套对象的错误消息