Rails Query 选择最新记录的最佳方法

发布于 2025-01-07 13:16:09 字数 654 浏览 0 评论 0原文

我有这个方法,我希望我从跟踪所有用户的模型 UserActivity 中获取相应 user_id 的最新 activity_id活动,并从中获取另一个模型 Activity 的 Activity_id 的 description。 该函数工作正常,但有时会抛出“无法找到 Activity_id”的错误。可能有更好的方法来实现这个或者我在这里做错了什么吗?

def last_activity(date, user_id)
  active_id = UserActivity.find(:all, :conditions => ["Date(updated_at) <= ? and user_id = ?", date, user_id]).last.activity_id
  text = Activity.find(active_id).description
end

模型UserActivity的架构如下: 在此处输入图像描述

请帮助我解决此问题。谢谢 !!

Ruby 版本 1.8.7、Rails 2.1.2

I have this method which I want me to fetch the most recent activity_id for the corresponding user_id from the model UserActivity that keeps track of all the user activities and from that get the description of the activity_id from another model Activity.
The function works fine but at times throws bugs as "Not able to find activity_id". May be there could be a better way to implement this or Am I doing something wrong here ?

def last_activity(date, user_id)
  active_id = UserActivity.find(:all, :conditions => ["Date(updated_at) <= ? and user_id = ?", date, user_id]).last.activity_id
  text = Activity.find(active_id).description
end

The Schema of the model UserActivity is as follows :
enter image description here

Please help me get this fixed. Thanks !!

Ruby version 1.8.7, Rails 2.1.2

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

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

发布评论

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

评论(2

以可爱出名 2025-01-14 13:16:09

假设您有以下模型

class User
  has_many :user_actvities
  has_many :activities, :through => :user_actvities, 
    :order => "user_actvities.id"
end

class UserActivity
 belongs_to :user
 belongs_to :activity
end

class Actvity
  has_many :user_actvities
  has_many :users, :through => :user_actvities
end

获取当前用户的最后一个活动

current_user.activities.last

这依赖于具有最大 id 的 UserActivity 对象是最新活动的事实。

如果创建后更新UserActivity,则必须更改关联的order子句,即

has_many :activities, :through => :user_actvities, 
  :order => "user_actvities.updated_at"

Assuming you have following models

class User
  has_many :user_actvities
  has_many :activities, :through => :user_actvities, 
    :order => "user_actvities.id"
end

class UserActivity
 belongs_to :user
 belongs_to :activity
end

class Actvity
  has_many :user_actvities
  has_many :users, :through => :user_actvities
end

To get the current user's last activity

current_user.activities.last

This relies on the fact that UserActivity object with the max id is the latest activity.

If you update the UserActivity after creation, you have to change the order clause of the association, i.e.

has_many :activities, :through => :user_actvities, 
  :order => "user_actvities.updated_at"
假扮的天使 2025-01-14 13:16:09

查询未找到任何匹配项,或者某些记录的 Activity_id 列可能具有 null 值,以下代码应该有效

user_activity = UserActivity.find(
   :all, :conditions => ["DATE(updated_at) <= ? AND user_id = ? AND activity_id IS NOT NULL", date, user_id], 
   :order => "updated_at DESC", :limit => 1).first
text = Activity.find(user_activity.active_id).description rescue nil

Either the query is not finding any match or some records might be having null values for activity_id column, following code should work

user_activity = UserActivity.find(
   :all, :conditions => ["DATE(updated_at) <= ? AND user_id = ? AND activity_id IS NOT NULL", date, user_id], 
   :order => "updated_at DESC", :limit => 1).first
text = Activity.find(user_activity.active_id).description rescue nil
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文