【问题标题】:How do I write a named_scope for one resource through another?如何通过另一个资源为一个资源编写 named_scope?
【发布时间】:2010-11-24 04:13:45
【问题描述】:

我有一个belongs_to Workouts 的团体资源。锻炼可以是公共的或私人的,并且由锻炼表中的列share 指定(如果锻炼是公共的,它是一个整数,包含 1)。

我正在尝试呼叫与公共锻炼相关的所有组。我认为这需要通过named_scope 完成,但我不确定语法。

在 groups_controller 中,我假设我会调用:

@groups = Group.public_groups.all

我应该如何在 Group.rb 中写named_scope? (我在 Rails 2.3.8 中)

named_scope :public_groups, ...

【问题讨论】:

    标签: ruby-on-rails ruby named-scope


    【解决方案1】:

    这是一种方法:

    class Group < ActiveRecord::Base
      belongs_to :workout
      named_scope :public, {:conditions => 'workouts.share = 1', :include => :workout}
    end
    
    @groups = Group.public.all
    

    【讨论】:

    • 很棒的人。工作得很好。只是还没有那么熟悉 named_scope。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多