关联'has_many'和'has_one'具有相同的模型

时间:2013-04-08 05:43:37

标签: ruby-on-rails ruby-on-rails-3 ransack

我有一个具有很多价格的Fabric模型。

    Class Fabric < ActiveRecord::Base  
        has_many :prices, :as => :priceable, :dependent => :destroy
    end

    class Price < ActiveRecord::Base
        attr_accessible :amount,
                :quoted_date,
                :valid_till

        belongs_to :priceable, :polymorphic => true
    end

如何添加关联以提供latest_price?

我需要在搜索结果搜索表单中为Fabrics添加价格范围搜索条件,该搜索表单应返回给定范围内的latest_price_amount的所有结构,其中LatestPrice是具有最近quoted_date的价格。

    <%= search_form_for @search do |f| %>
        <%= f.text_field :latest_price_amount_gteq %>
        <%= f.text_field :latest_price_amount_lteq %>
    <%= f.submit "Search" %>

我已经研究过this question,第一种方法对我来说似乎很好,但是我可以用has_one替换belongs_to,因为Fabric可能有也可能没有价格?如果没有,你能否提出更好的解决方案。

感谢。

0 个答案:

没有答案
相关问题