HABTM 检查最新 3 项的匹配
对于你们来说,这是一个有趣的关系......
我在“狗”和“旅行”之间有一个 HABTM(has_and_belongs_to_many)关系。我的目标是找到两个结果集: 1) 过去 3 次旅行中至少有 1 次的狗,并称其为 @dogs_current 2) 没有参加过最近 3 次旅行的狗并称其为 @dogs_old
我发现我可以通过在 Trip 模型中执行此操作来找到最近 3 次旅行:
named_scope :last3, :order => 'date DESC', :limit => 3
但不确定如何使用该列表 get 1 和2. 这个 hack 有效,但看起来很丑。一定有更好的办法! :)
@dogs_current = []
@dogs_old = []
@dogs.each do |dog|
if (Trip.last3 - dog.trips).size < 3 then
@dogs_current << dog
else
@dogs_old << dog
end
end
有什么想法吗?谢谢! -凸轮
Here's an interesting one for you folks...
I have a HABTM (has_and_belongs_to_many) relationship between "Dogs" and "Trips". My goal is to find two result sets:
1) Dogs that have been on at least 1 of the last 3 trips and call that @dogs_current
2) Dogs that have NOT been on any of the last 3 trips and call that @dogs_old
I found that I can find what the last 3 trips are by doing this in the Trip model:
named_scope :last3, :order => 'date DESC', :limit => 3
But not sure how to use that list get 1 and 2. This hack works, but it seems ugly. There must be a better way! :)
@dogs_current = []
@dogs_old = []
@dogs.each do |dog|
if (Trip.last3 - dog.trips).size < 3 then
@dogs_current << dog
else
@dogs_old << dog
end
end
Any ideas? Thanks!
-Cam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这是正确的。至少对我来说在这里工作......
I believe this is correct. At least worked here for me...