Rails 3:具有方法调用和模型关联的 named_scope 的正确语法 [英] Rails 3: Correct syntax for named_scope with method call and model associations

查看:42
本文介绍了Rails 3:具有方法调用和模型关联的 named_scope 的正确语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有四个模型,定义如下

I have four models in my application, defined as follows

class User < ActiveRecord::Base
    has_many :comments
    has_many :geographies
    has_many :communities, through: :geographies

class Comment < ActiveRecord::Base
    belongs_to :user

class Community < ActiveRecord::Base
    has_many :geographies
    has_many :users

class Geography < ActiveRecord::Base
    belongs_to :user
    belongs_to :community

用户可以发表评论,这些评论通过地理表与一个或多个社区相关联.

Users can post comments, which are associated with one or more communities through the geography table.

我的任务是仅显示来自下拉列表中选择的社区的评论.我从 这篇文章 中了解到,我可以访问通过 comment.user.communities.first 对象链的给定评论的社区.

My task is the display only comments from the community selected from a dropdown list. I learned from this post that I can access the community of a given comment via the comment.user.communities.first object chain.

似乎通常带有 lambda 的 named_scope 将是过滤所有评论列表的首选,但是,我完全不知道如何构造这个 named_scope.我试图通过遵循一些 RailsCast 来构建 named_scope,但这是我所能得到的.生成的错误如下.

It seems typically a named_scope with lambda would be the preferred choice to filter the list of all comments, however, I am at a complete loss how to construct this named_scope. I've tried to construct the named_scope by following some RailsCasts, but this is as far as I was able to get. The generated error is below.

class Comment < ActiveRecord::Base
    belongs_to :user

    def self.community_search(community_id)
        if community_id
            c = user.communities.first
            where('c.id = ?', community_id)
        else 
            scoped
        end
    end

    named_scope :from_community, { |*args| { community_search(args.first) } }

这是错误:

syntax error, unexpected '}', expecting tASSOC
named_scope :from_community, lambda { |*args|  { community_search(args.first) } }
                                                            ^

将带参数的方法传递到 named_scope 的正确语法是什么?

What is the correct syntax for passing a method with arguments into a named_scope?

推荐答案

首先,你现在可以在 rails 3 中使用 scope - 旧的 named_scope 形式被缩短了,它在 Rails 3.1 中删除

First, you can just use scope now in rails 3 - the older named_scope form was shortened, and it's removed in rails 3.1!

不过,关于您的错误,我怀疑您不需要内部括号.当使用这样的 lambda 块时,它们通常会翻倍,因为您是从头开始创建新的哈希,如下所示:

With regard to your error, though, I suspect you don't need the inner set of brackets. They are usually doubled when using a lambda block like that, because you are creating new hash from scratch, like this:

scope :foo, { |bar|
  { :key => "was passed #{bar}" }
}

不过,在您的情况下,您正在调用 community_search,它应该返回一个您可以直接返回的值.在这种情况下,一个 AREL 对象已经替换了这种简单的哈希值.在阅读有关该主题的所有随机帖子和教程时,这有点令人困惑,这主要是由于 AREL 导致风格发生了巨大变化.不过,这两种样式都可以使用 - 作为 lambda 或类方法.它们在很大程度上意味着相同的事情.上面的两个链接有几个这种新样式的例子供进一步阅读.

In your case, though, your are calling community_search which should be returning a value that you can return directly. In this case, an AREL object that has replaced such simple hashes. It's somewhat confusing when reading all the random posts and tutorials out there on this subject, largely due to the huge change in style that AREL caused. Both of those style use use are ok, though - either as a lambda or class method. They largely mean the same thing. The two above links have several examples of this newer style for further reading.

当然,你可以学习像 squeel 这样的东西,我觉得它更容易阅读,并删除了很多打字.^^;

Of course, you could just learn something like squeel, which I find much easier to read, and cuts out a lot of the typing. ^^;

这篇关于Rails 3:具有方法调用和模型关联的 named_scope 的正确语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆