[英]Ruby on Rails - accessing nested attributes of current user
i am trying to access some nested attributes in a rails partial; 我正在尝试访问部分Rails中的嵌套属性; let me explain my issue:
让我解释一下我的问题:
i wrote a simple messaging service according to the tutorial of dana mulder i mentioned in another question of mine. 我根据我在另一个问题中提到的dana mulder的教程编写了一个简单的消息服务。 a
user
has_many conversations
, and a conversation
has_many messages
. user
has_many conversations
,而conversation
has_many messages
。 every conversation has a recipient and a sender and every message has a boolean read
, which is set to false
by default. 每个对话都有一个收件人和一个发件人,每个消息都有一个布尔型
read
,默认情况下将其设置为false
。
what i am trying to do is to write a simple function that iterates through every message a user got in a conversation he is involved in, checks if the last message a user recieved in a conversation is read == true and not send by himself to display a little knob beside the "messages" link in the navigation of my app. 我要做的是编写一个简单的函数,该函数遍历用户所参与的对话中的每条消息,检查用户在对话中收到的最后一条消息是否读为== true,而不是自己发送给在我的应用程序导航中的“消息”链接旁边显示一个小旋钮。
What i basically want to check is (multiple lines for better readability): 我基本上要检查的是(多行以提高可读性):
<%= if current_user.conversations.all.messages.last.user_id != current_user.id && current_user.conversations.all.messages.last.read == false %>
Message link with bubble beside it
<% else %>
Message link
<% end %>
This syntax does not work .. How is it possible to iterate through every conversation a user is involved, check the last message of every conversation if its written by this user and if not if its already read? 此语法不起作用..如何遍历用户所涉及的每个会话,检查每个会话的最后一条消息(如果此用户写的消息,如果不是已读的消息)?
while something like Conversation.last.messages.last.content
is working, current_user.conversations.all.messages.last.content
is not.. i find it a bit confusing how the accessibility of rails models are working. 虽然像
Conversation.last.messages.last.content
这样的东西正在工作,但current_user.conversations.all.messages.last.content
却没有..我发现它有点困扰Rails模型的可访问性。
thanks in advance! 提前致谢! i hope i was explaining me well enough.
我希望我对我的解释足够好。
Best Regards 最好的祝福
What are you trying to do with current_user.conversations.all.messages.last.content
? 您要如何处理
current_user.conversations.all.messages.last.content
? because conversations.all
is an association, it is NOT a Conversation, so messages
is not an available method. 因为
conversations.all
是关联,所以它不是对话,因此messages
不是可用的方法。 Conversation.last
IS a Conversation, that's why you can call messages
on it. Conversation.last
是一种对话,因此您可以在其上调用messages
。
You can try current_user.conversations.last.messages.last.content
to get the last message of the last conversation, or, you can use a "has_many :through" relationship on User 您可以尝试使用
current_user.conversations.last.messages.last.content
来获取上次对话的最后一条消息,也可以在User上使用“ has_many:through”关系
class User ...
has_many :conversations
has_many :messages, through: :conversations
end
That way you can do current_user.messages
if you want ALL user's messages and current_user.messages.last
to get the last Message (even if it's not from the last conversation). 这样,如果您希望所有用户的消息和
current_user.messages.last
都获得最后一条消息(即使它不是来自上一次对话),则可以执行current_user.messages
。
I'd recommend you to read the Rails Guide about Associations https://guides.rubyonrails.org/association_basics.html#the-has-many-through-association 我建议您阅读有关关联的Rails指南https://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
EDIT: To know if the user has some unread message, you can use the has_many :through
association and do something like 编辑:要知道用户是否有未读消息,可以使用
has_many :through
关联并执行类似的操作
current_user.messages.where(read: false).where.not(user_id: current_user.id).any?
That will return true
any message from the user's conversations with id != current_user.id is not read. 这将返回
true
则不会读取用户对话中ID为!= current_user.id的任何消息。 And false
otherwise. 否则为
false
。
You can use count
instead of any?
您可以使用
count
代替any?
if you wan't to know the actual number of unread messages. 如果您不想知道未读邮件的实际数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.