【问题标题】:Only show submissions for the current festival只显示当前节日的提交
【发布时间】:2013-10-29 22:52:24
【问题描述】:

我的节日和提交申请中有模型。每个提交都属于一个节日。我有一个助手可以根据每个节日的日期确定什么是“current_festival”。

module ApplicationHelper

  def current_festival
    @current_festival ||= Festival.current.first
  end

end

.current 是节日模型中的一个作用域:

  scope :current, where("published = true and closing_date >= ?", Time.now.to_date).order('closing_date asc').limit(1)

我希望将索引视图中显示的提交限制为仅属于当前节日的提交。你将如何在控制器中做到这一点?或者最好在模型中以某种方式做到这一点,也许通过一个范围?

【问题讨论】:

    标签: ruby-on-rails-3 model controller scope belongs-to


    【解决方案1】:

    我想你有一个这样定义的关系:

    class Festival
      has_many :submissions
    end
    

    那么你可以在任何地方做:

    Festival.current.submissions
    

    【讨论】:

    • 好吧,我认为实际的 scope 可能会返回一个 collection 节日物品,即使它是一个节日物品的集合,所以,如果上面的代码失败了,试试这个:Festival.current.first.submissions
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2021-09-07
    • 2017-11-04
    • 1970-01-01
    • 2020-01-11
    • 2014-05-22
    • 2015-12-08
    相关资源
    最近更新 更多