在rails上一次更新多个属性 [英] Updating more than one attribute at one time in ruby on rails

查看:116
本文介绍了在rails上一次更新多个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,用户可以在当前的线索对话中看到消息列表。

他们可以使用删除链接(每个消息的每个循环内的链接)删除每条消息:

 <%= link_to'删除',message,:method => :删除,:确认=> 你确定吗?,:title => message.body%> < br /> 

这基本上将它们连接到我的messages_controller中的一个动作:

  def destroy 

@message = Message.find(params [:id])
@message .update_attribute('sender_status',1)if @ message.sender_id == current_user.id
@ message.update_attribute('recipient_status',1)if @ message.recipient_id == current_user.id
@message .destroy if @ message.sender_status == 1&& @ message.recipient_status == 1

flash [:success] =Message deleted
redirect_to:back
end

现在我想要的是在所有消息之前的一个链接(所以我不会把它放在每个块中)。该链接将被称为全部删除,而不是使用值1更新1属性,它将所有属性(sender / recipient_status)更新为1,以便用户不必单击每个单独的消息删除链接。 / p>

我有点卡住了如何做到这一点。我不使用复选框,而是删除链接,以便删除所有我宁愿使用链接。



任何机构都可以为我提供解决方案?我真的很感激它。我以为我昨天晚上睡觉,早上有个主意,但仍然没有想到。



亲切的问候



更新



这些讯息属于对话。我可以通过以下方式获取对话:
1)MessageThread表。这将会话中第一条消息的message_id存储为该对话中其他消息的parent_id。举例来说。


  + ----- + ---------- + ---- ----- + --------- + --------- + -------- + --------- + ----- ----- + ---------- + ---------- + 
| id | sende ... | reci ... | body | pare ... |状态| crea ... | updat ... | sende ... | recip ... |
+ ----- + ---------- + --------- + --------- + -------- - + -------- + --------- + ---------- + ---------- + ------- --- +
| 220 | 4 | 1 |嗨| 220 | 0 | 2012 ... | 2012 -... | 0 | 0 |
| 221 | 1 | 4 |嘿| 220 | 0 | 2012 ... | 2012 -... | 0 | 0 |
| 222 | 4 | 1 |嗨| 220 | 0 | 2012 ... | 2012 -... | 0 | 0 |
| 223 | 1 | 4 |嗨| 220 | 0 | 2012 ... | 2012 -... | 0 | 0 |
| 232 | 1 | 4 |好| 220 | 0 | 2012 ... | 2012 -... | 0 | 0 |
| 233 | 4 | 1 |和... | 220 | 0 | 2012 ... | 2012 -... | 0 | 0 |
| 237 | 1 | 4 |嗯| 220 | 0 | 2012 ... | 2012 -... | 0 | 0 |
| 238 | 1 | 4 | yay | 220 | 0 | 2012 ... | 2012 -... | 0 | 0 |
| 239 | 1 | 4 | yess | 220 | 0 | 2012 ... | 2012 -... | 0 | 0 |

2)我也可以通过父母id找到对话。例如m = Message.find_by_parent_id(220)。



在convo中获取其他消息我只需键入m.children



以下是我的messagethreads表看起来:

  + ----- + ------------ + --- -------- + -------------- + -------- + ----------------- ---- + --------------------- + 
| id | message_id | sender_id | recipient_id |状态| created_at | updated_at |
+ ----- + ------------ + ----------- + -------------- + -------- + --------------------- + ------------------ --- +
| 102 | 247 | 1 | 5 | 0 | 2012-02-16 16:18 ... | 2012-02-16 16:18 ... |
| 101 | 246 | 1 | 3 | 0 | 2012-02-16 16:14 ... 2012-02-16 16:14 ...
| 90 | 220 | 4 | 1 | 0 | 2012-02-15 14:05 ... | 2012-02-15 14:05 ... |
| 89 | 219 | 4 | 38 | 0 | 2012-02-15 12:26 ... | 2012-02-15 12:26 ... |
| 88 | 218 | 2 | 4 | 0 | 2012-02-14 13:41 ... | 2012-02-14 13:41 ... |
| 87 | 210 | 1 | 2 | 0 | 2012-02-14 13:31 ... | 2012-02-14 13:31 ... |
+ ----- + ------------ + ----------- + -------------- + -------- + --------------------- + ------------------ --- +

以下是我的工作内容:

  def destroy_all_messages 

Message.find_by_parent_id(params [:format])。children.each do | message |
message.update_attribute('sender_status',1)if message.sender_id == current_user.id
message.update_attribute('recipient_status',1)if message.recipient_id == current_user.id
message.destroy if message.sender_status == 1&& message.recipient_status == 1
end
flash [:success] =消息已删除
redirect_to:back
end

和:

 <%= link_to'全部删除',messages_delete_all_messages_path(@ current_thread.message_id),:method => :删除,:确认=> 你确定? %GT; < br /> 

这就是我的工作。意见?
我注意到我不得不使用params [:format]来获取我在控制器中传递给link_to消息路径的参数。

解决方案

这些消息如何分组?他们是否属于对话?



如果它是一个对话(with has_many:messages)您可以添加一个删除所有消息按钮到发送ID这个对话到一个delete_messages动作。在这个动作中做

$ $ p $ def destroy_messages

Conversation.find(params [:id])。messages .each do | message |
message.update_attribute('sender_status',1)if message.sender_id == current_user.id
message.update_attribute('recipient_status',1)if message.recipient_id == current_user.id
message.destroy if message.sender_status == 1&& message.recipient_status == 1
end
end

可能需要一些详细信息适当的授权。如果没有对话模型,则在搜索要删除的消息后执行类似操作。 (或告诉我们一些细节,你显示的是什么以及你决定如何显示它)



更新



这应该是一样的。如果您可以轻松获取所有消息,只需将对话替换为父消息即可。你的链接应该发送父消息的ID,然后运行它的所有子消息并更新其状态。应该很容易。

  m = Message.find(params [:id])
m.children.each do | message |
...同上...
结束

一般来说,发送一个足以识别要删除的消息的标识符(在这种情况下为父代的标识符),使用它来运行查找并更新记录。


I have a page where a user can see a list of messages in a current threaded conversation.

They can delete each message using a delete link (a link inside an each loop for each message):

<%= link_to 'Delete', message, :method => :delete, :confirm => "Are you sure?", :title => message.body %> <br />

This basically connects them to an action in my messages_controller:

  def destroy

  @message = Message.find(params[:id])
  @message.update_attribute('sender_status', 1) if @message.sender_id == current_user.id
  @message.update_attribute('recipient_status', 1) if @message.recipient_id == current_user.id
  @message.destroy if @message.sender_status == 1 && @message.recipient_status == 1

  flash[:success] = "Message deleted"
    redirect_to :back
  end

What I'd like to have now is one link before all the messages (so I won't put this in the each block). This link will be called "delete all" and rather than updating 1 attribute with the value 1 it will update all the attributes (sender/recipient_status) to 1 so that a user doesn't have to click on each individual messages delete link.

I'm a bit stuck with how to do this. I don't use checkboxes but rather delete links so for the delete all I'd rather use a link too.

Can any body offer me a solution with an example please? I'd really appreciate it. Thought I'd sleep on it last night and have an idea in the morning but still nothing comes to mind.

Kind regards

Update

The messages belong to a conversation. I can either grab a conversation by: 1) The MessageThread table. This stores the message_id of the first message in the conversation which is the parent_id of the other messages in that conversation. So for example.

+-----+----------+---------+---------+---------+--------+---------+----------+----------+----------+
| id  | sende... | reci... | body    | pare... | status | crea... | updat... | sende... | recip... |
+-----+----------+---------+---------+---------+--------+---------+----------+----------+----------+
| 220 | 4        | 1       | hi      | 220     | 0      | 2012... | 2012-... | 0        | 0        |
| 221 | 1        | 4       | hey     | 220     | 0      | 2012... | 2012-... | 0        | 0        |
| 222 | 4        | 1       | hi      | 220     | 0      | 2012... | 2012-... | 0        | 0        |
| 223 | 1        | 4       | hi      | 220     | 0      | 2012... | 2012-... | 0        | 0        |
| 232 | 1        | 4       | good    | 220     | 0      | 2012... | 2012-... | 0        | 0        |
| 233 | 4        | 1       | and ... | 220     | 0      | 2012... | 2012-... | 0        | 0        |
| 237 | 1        | 4       | hmm     | 220     | 0      | 2012... | 2012-... | 0        | 0        |
| 238 | 1        | 4       | yay     | 220     | 0      | 2012... | 2012-... | 0        | 0        |
| 239 | 1        | 4       | yess    | 220     | 0      | 2012... | 2012-... | 0        | 0        |

2) I can also just find the conversation by parent id. e.g. m = Message.find_by_parent_id(220).

to get the other messages in the convo I would just type m.children

Here is how my messagethreads table looks:

+-----+------------+-----------+--------------+--------+---------------------+---------------------+
| id  | message_id | sender_id | recipient_id | status | created_at          | updated_at          |
+-----+------------+-----------+--------------+--------+---------------------+---------------------+
| 102 | 247        | 1         | 5            | 0      | 2012-02-16 16:18... | 2012-02-16 16:18... |
| 101 | 246        | 1         | 3            | 0      | 2012-02-16 16:14... | 2012-02-16 16:14... |
| 90  | 220        | 4         | 1            | 0      | 2012-02-15 14:05... | 2012-02-15 14:05... |
| 89  | 219        | 4         | 38           | 0      | 2012-02-15 12:26... | 2012-02-15 12:26... |
| 88  | 218        | 2         | 4            | 0      | 2012-02-14 13:41... | 2012-02-14 13:41... |
| 87  | 210        | 1         | 2            | 0      | 2012-02-14 13:31... | 2012-02-14 13:31... |
+-----+------------+-----------+--------------+--------+---------------------+---------------------+

Here is what is working for me:

 def destroy_all_messages

  Message.find_by_parent_id(params[:format]).children.each do |message|
    message.update_attribute('sender_status', 1) if message.sender_id == current_user.id
    message.update_attribute('recipient_status', 1) if message.recipient_id == current_user.id
    message.destroy if message.sender_status == 1 && message.recipient_status == 1
  end
        flash[:success] = "Messages deleted"
    redirect_to :back
end

and:

<%= link_to 'Delete All', messages_delete_all_messages_path(@current_thread.message_id), :method => :delete, :confirm => "Are you sure?" %> <br />

This is what's working for me.. Opinions? I noticed I had to use "params[:format]" to grab the params I passed into the link_to message path in my controller.

解决方案

How are those messages grouped? Do they belong to a Conversation?

If it's a Conversation (with has_many :messages) You could add a "delete all messages" button to the conversation that sends the id of this conversation to a delete_messages action. In this action do

def destroy_messages

  Conversation.find(params[:id]).messages.each do |message|
    message.update_attribute('sender_status', 1) if message.sender_id == current_user.id
    message.update_attribute('recipient_status', 1) if message.recipient_id == current_user.id
    message.destroy if message.sender_status == 1 && message.recipient_status == 1
  end
end

Could need some details for proper authorization. If there is no Conversation model, do something similar after searching for the messages to delete. (Or tell us some details what exactly you display and how you decide to display it)

Update

This should be just the same. If you can get all messages that easy, just replace "Conversation" with the parent message. You link should send the id of the parent message and then run through all of it's child messages and update their status. Should be easy enough.

m = Message.find(params[:id])
m.children.each do |message|
    ... as above ...
end

In general, just send an identifier (id of parent in this case) that is good enough to identify the messages to be deleted, use it to run a find and then update the records.

这篇关于在rails上一次更新多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆