Ruby on Rails:使用嵌套的named_scopes
我刚刚被一位朋友推荐到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
named_scopes
看起来不错。除非您在not_awarded_medal
条件语句中以单撇号开头并以双撇号结尾。编辑:
收回它。您的
not_awarded_medal
named_scope 已关闭。尝试这样的事情:
未经测试
现在假设您具有以下关系:
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 thenot_awarded_medal
condition statement.EDIT:
Take it back. Your
not_awarded_medal
named_scope is off.Try something like this:
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 ismedal
defined?If
comments
is an array then you need to passcomments.length
intofrequent_comment_club_members
. If it's the count then I would rename it tocomments_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)Medal.find_by_id(medal.id)
? You already have the medal object.