Ruby on Rails:使用嵌套的named_scopes

发布于 2024-08-29 18:23:20 字数 1218 浏览 4 评论 0原文

我刚刚被一位朋友推荐到 stackoverflow 来帮助解决我遇到的问题。我对 ruby​​ on Rails 相当陌生,我正在开发一个协作项目,其中我们有一个脚本 (medal_worker.rb),该脚本计划以固定的时间间隔运行,根据我们网站上的各种参与和成功情况向人们授予各种奖牌。我正在制作的一枚新奖章奖励人们的“里程碑”。出于此问题的目的,假设我们要在他们发表 100、1000 和 10000 条评论时授予他们奖牌。我想通过使用用户模型(user.rb)中的named_scopes来为我提供我正在寻找的用户的过滤列表来做到这一点。

我的问题是:如何找到没有相应里程碑评论级别相应奖牌的用户(最好使用用户模型中的named_scopes)?

这里是我的 model_worker.rb 文件摘录:

def award_comment_milestone(comments)
   users = Users.frequent_comment_club_members(comments).not_awarded_medal(Medal.find_by_id(medal.id))
   for user in users do
      award_medal(medal, nil, user) if @award
   end
end

这是我在用户模型(user.rb)中使用named_scopes的情况:

named_scope :frequent_comment_club_members, lambda { |*args|
        {:include => comment_records, :conditions => ['comment_records.comment_type = ? and comment_records.comments >= ?', 'User', (args.first || 0)]}
}

named_scope :not_awarded_medal, lambda { |medal|
    {:include => :awards, :conditions => ['awards.medal_id not in (select awards.medal_id from awards where awards.medal_id = ?)", medal.id] }
}

这没有按我想要的方式工作,但我不知道问题是否出在named_scopes中或者我如何传递争论或者什么。谢谢。

I just got referred to stackoverflow by a friend here to help with a problem I am having. I am fairly new to ruby on rails and I am working on a collaborative project where we have a script (medal_worker.rb) that is scheduled to run at a fixed intervals to award people various medals based on various participation and success on our website. One of the new medals I am working on rewards people for "milestones". For the purpose of this problem, let's say we want to give them medals when they make 100, 1000, and 10000 comments. I would like to do this by using named_scopes from the User model (user.rb) to give me filtered lists of the users I am looking for.

My question is: How do I find the users who do not have the respective medals for the respective milestone comment level (preferably using the named_scopes from the User model)?

Here is an exerpt from my model_worker.rb file:

def award_comment_milestone(comments)
   users = Users.frequent_comment_club_members(comments).not_awarded_medal(Medal.find_by_id(medal.id))
   for user in users do
      award_medal(medal, nil, user) if @award
   end
end

Here is where I am at with the named_scopes in the user model (user.rb):

named_scope :frequent_comment_club_members, lambda { |*args|
        {:include => comment_records, :conditions => ['comment_records.comment_type = ? and comment_records.comments >= ?', 'User', (args.first || 0)]}
}

named_scope :not_awarded_medal, lambda { |medal|
    {:include => :awards, :conditions => ['awards.medal_id not in (select awards.medal_id from awards where awards.medal_id = ?)", medal.id] }
}

This is not working as I would like, but I don't know if the problem is in the named_scopes or how I am passing arguements or what. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好久不见√ 2024-09-05 18:23:20

您的 named_scopes 看起来不错。除非您在 not_awarded_medal 条件语句中以单撇号开头并以双撇号结尾。

编辑

收回它。您的 not_awarded_medalnamed_scope 已关闭。

尝试这样的事情:

named_scope :not_awarded_medal, lambda { |medal_id|
  { :include => :awards,
    :conditions => [
      "? not in (select awards.id from awards where awards.user_id = users.id)", medal_id
    ]
  }
}

未经测试

现在假设您具有以下关系:

User: has_many :awards
Award:Belongs_to :user

如果您使用has_many :through,那么您将必须更改 SQL 以查看联接 (users_awards) 表。

--

但我确实在 award_comment_milestone 函数中看到了一些东西。

award_comment_milestone 中输入的参数是什么?是评论数组还是评论计数?另外,medal 是在哪里定义的?

如果 comments 是一个数组,那么您需要将 comments.length 传递到 frequent_comment_club_members 中。如果是计数,那么我会将其重命名为 comments_count,以便下一个人可以更快地理解逻辑。

一些一般性观察:

  • not_awarded_medal 应该只获取 Medal_id 而不是整个对象(无需执行多个查询)
  • 为什么要执行 Medal.find_by_id(medal.id)?您已经拥有奖牌对象。

Your named_scopes look fine. Except you are starting with a single apostrophe and ending with a double apostrophe in the not_awarded_medal condition statement.

EDIT:

Take it back. Your not_awarded_medal named_scope is off.

Try something like this:

named_scope :not_awarded_medal, lambda { |medal_id|
  { :include => :awards,
    :conditions => [
      "? not in (select awards.id from awards where awards.user_id = users.id)", medal_id
    ]
  }
}

untested

Now this is assuming that you have the following relationships:

User: has_many :awards
Award: belongs_to :user

If you are using has_many :through then you are going to have to change the SQL to look at the join (users_awards) table.

--

But I do see a couple of things in the award_comment_milestone function.

What is the parameter coming into award_comment_milestone? Is it an array of comments or is it a count of comments? Also where is medal defined?

If comments is an array then you need to pass comments.length into frequent_comment_club_members. If it's the count then I would rename it to comments_count so the next person can understand the logic more quickly.

Some general observations:

  • not_awarded_medal should just take a medal_id and not the whole object (no need to do multiple queries)
  • Why are you doing Medal.find_by_id(medal.id)? You already have the medal object.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文