我有Person<ActiveRecord::Base
型号
class Person < ActiveRecord::Base
has_many :sent_messages, :class_name => 'Message', :inverse_of => :sender
has_many :received_messages, :class_name => 'Message', :inverse_of => :receiver
#.....
end
和另一个modeActiveRecord :: Base模型
class Message < ActiveRecord::Base
belongs_to :sender, :class_name => 'Person'
belongs_to :receiver, :class_name => 'Person'
# ......
end
和ActiveRecord :: Migration类支持这些模型。现在我的问题是我需要通过查询数据库输出当前用户收到的messages
Person name
发件人,但在查询时,似乎发送邮件的某人(用户)已从数据库中删除(但是一旦交换了消息,两个人就永久保存在数据库中,并且没有机构有权删除它,因此每条消息都会包含receiver_id
和{{1}等所有详细信息}。)。所以当我这样查询时。
sender_id
如果将消息发送给当前登录用户的人的@messages = Message.where(:receiver_id => current_user.id)
.includes(:sender).order("updated_at DESC")------------(1)
属性为null(意味着没有从数据库中删除,并且可以在delete_at
的视图中使用而没有任何错误),它可以正常工作对于已删除的内容,它会在浏览器控制台中显示此错误
sender name
其中一个方法在应用程序控制器中处理没有找到记录的异常
in application controller asset is undefined method or variable ----------(2)
请告诉我def respond_to_not_found(*types)
flash[:warning] = t(:msg_asset_not_available, asset)
respond_to do |format|
format.html { redirect_to(redirection_url) }
# ...
end
end
和t
是什么?
作为问题asset
的解决方法--------(3)。
我查询了这个和
(1)
我的问题: -
Q.1)我没有太多使用数据库,所以这对我来说是有效的问题(因为我正在研究的项目明确地破坏了所请求的用户): - 为什么用户信息还没有从数据库中删除而是它的def show_list
@messages = Array.new
@person = Array.new
messages_temp = Message.where(:receiver_id => current_user.id).
includes(:sender).order("updated_at DESC")
i = 0
messages_temp.each do |msg|
if(!(msg.sender.nil?))
@messages[i] = msg
@person[i] = msg.sender
i = i+1
end
end
end
属性已成为deleted_at
,而其他完整的属性则具有相同的属性not null
。当我null
(按方法find
查询)来自数据库的find
对象然后在查询它时,Person
属性的SQL转换查询应为deleted_at
。那么为什么我的项目代码中遗漏了一些内容,或者这是一般行为。如果它一般,那么如何完全删除数据。
即使这种行为是一般的或不是如何恢复它,也不必手动将每个已删除的Person null
属性更改为null。以及如何在不完全恢复Person的情况下访问其中一些属性(或者如果可能的话)。
Q.2)即使在解决方法之后,可能还有其他原因导致错误#(2)。当我测试解决方法时,它对一些已删除的发件人工作正常。但即使在某些地方我得到错误#(2)所以一般来说这个错误代表什么,可能是它的另一个原因那么我提到的那个???
问:3)是否有更好的解决方案,然后在方程#(3)中给出的解决方法,因为您在我的解决方法中看到deleted_at
对于已删除的对象是正确的,所以我认为可能有一些。我试过网但没有成功。那么,如何过滤掉那些删除了相关对象的消息,如何一次性查询它们,然后像#(2)一样逐一查询。
由于解决方法是让我很难对输出进行分页,因为所有可用的msg.sender.nil?
(例如“pagination gem
”,“will_paginate
”,“kaminari
”都适用于{ {1}}但在我的解决方法中,我有一个Array对象,即pagination
,我无法使用这些方法。因此,如果一个人可以回答分页我的解决方法,即对象ActiveRelation
这是一个数组,或者过滤掉那些从数据库中删除了发件人的邮件然后我可以用这种方式进行分页,那将会很有帮助:
@messages
PS: - 我知道这个问题很乏味但我相信这对我要回答的这个问题至关重要。即使是问题的部分答案,任何帮助将不胜感激。
非常感谢!!!
答案 0 :(得分:0)
好的...
好吧,我花了几天时间,但在附近的一些帮助下,我确实找到了大部分混淆和含糊不清的答案:
答: -
1 :)有一种方法可以软删除隐藏在普通查询中的数据...问题一的大部分歧义可以从这个[link][1]
回答。此gem
( acts_as_paranoid )有助于软删除数据。是的,可以访问软删除对象的属性而无需恢复日期;并且数据也可以完全清除(链接中的所有内容)。我需要在我的代码库中进行的更改才能获得这些好处: -
在Person<ActiveRecord::Base
模型中
class Person < ActiveRecord::Base
# has_many :sent_messages, :class_name => 'Message', :inverse_of => :sender
# has_many :received_messages, :class_name => 'Message', :inverse_of => :receiver
has_many :sent_messages, :class_name => 'Message',
:inverse_of => [:sender, :sender_including_deleted]
has_many :received_messages, :class_name => 'Message',
:inverse_of => [:receiver, :receiver_including_deleted]
#.....
end
并在模型中ActiveRecord::Base
class Message < ActiveRecord::Base
belongs_to :sender, :class_name => 'Person'
belongs_to :receiver, :class_name => 'Person'
# Add these two lines:-
belongs_to :sender_including_deleted, :class_name => 'Person',
:foreign_key => 'sender_id', :with_deleted => true
belongs_to :receiver_including_deleted, :class_name => 'Person',
:foreign_key => 'receiver_id', :with_deleted => true
# ......
end
所以,添加这两行对我有用,但最重要的是宝石给了我很多帮助。
2 :)在(2)中获得错误的一个突出原因是,可能正在尝试直接访问可能被删除的对象的属性。假设Person
被删除(软件或硬件无关紧要,其ID为15),因此first_name = Person.find(15).first_name
会在person = Person.find(15)
if(!person.nil?) return first_name
不会产生错误,因为我们正在检查是否返回person
是否为零。如果使用gem acts_as_paranoid
删除Person,则soft deleted
(意味着deleted_at
属性为not null
。deleted_at(必须在迁移中添加此属性)属性为删除对象以获得清晰图片时更新,请参阅嵌入链接here)对象访问first_name = Person.with_delete.find(15)
这样的对象不会生成错误
Note:-
我不是其他原因。如果你能想到,请回答这部分。
3 :)虽然我没有直接回答3中提出的问题,但从现在起我可以访问邮件发件人信息,即使它们已被删除(以活动关系形式),所以订购和分页有效,但我当然想知道如果对象处于非活动状态,如何分页。
所以,我最好这样回答: -
def show_list
# @messages = Array.new
# @person = Array.new
# messages_temp = Message.where(:receiver_id => current_user.id).
# includes(:sender).order("updated_at DESC")
# i = 0
# messages_temp.each do |msg|
# if(!(msg.sender.nil?))
# @messages[i] = msg
# @person[i] = msg.sender
# i = i+1
# end
# end
# REPLACE ABOVE CODE BY THIS. THe following code will
# include deleted messages as well.
@person = Array.new
@messages = Message.where(:receiver_id =>current_user.id).
includes(:sender_including_deleted).
order("created_at DESC").paginate(params).readonly
i = 0
@messages.each do |msg|
@person[i] = msg.sender_including_deleted(:with_deleted => true)
i = i + 1
end
end
我希望偶然发现这个问题的人得到他们正在寻找的东西。如果他们没有得到我的答案或问题的任何部分,那么欢迎他们在这里发表评论,我将很乐意尽我所能回答。