在我的通讯表子句我有一些列:update_attribute使用其中红宝石
id | UserID | CommunicationMode | CommunicationDetail | Private
1 | 1 | Phone | 123456789 | 1
2 | 1 | Email | [email protected] | 1
而且我想用更新列值where子句中使用循环象下面这样:
create
@user_communication=Communication.where(:UserID => current_user.id)
if [email protected]_communication.blank?
@user_communication.each do |c|
if params[:ChkBx_Phone].to_i == 1
c.where("CommunicationMode == 'Phone'").update_attribute(:Private, "1")
elsif params[:ChkBx_Phone].to_i == 0
c.where("CommunicationMode == 'Phone'").update_attribute(:Private, "0")
end
if params[:ChkBx_Email].to_i == 1
c.where("CommuicationMode == 'Email'").update_attribute(:Private, "1")
elsif params[:ChkBx_Email].to_i == 0
c.where("CommunicationMode == 'Email'").update_attribute(:Private, "0")
end
end
end
end
我想检查上面,如果Phone
复选框被选中然后它更新Private
列值1
其他0
其中CommunicationMode is Phone
和电子邮件我想检查,如果Email
复选框是c hecked那么它更新Private
柱与价值1
其他0
其中CommunicationMode is Email
及以下Phone
和Email
复选框:
<table>
<% @user_communication.each do |c| %>
<tr>
<td>
<% if c.CommunicationMode == "Phone" and c.Private.to_s == "1" %>
<input type="checkbox" name="ChkBx_Phone"
id="ChkBx_Phone" value="1" checked = "checked">
<%= label(:lb_Phone, "Phone") %>
<% elsif c.CommunicationMode == "Phone" and c.Private.to_s == "0" %>
<%= check_box_tag 'ChkBx_Phone' %>
<%= label(:lb_Phone, "Phone") %>
<% end %>
</td>
</tr>
<tr>
<td>
<% if c.CommunicationMode == "Email" and c.Private.to_s == "1" %>
<input type="checkbox" name="ChkBx_Email"
id="ChkBx_Email" value="1" checked = "checked">
<%= label(:lb_Email, "Email") %>
<% elsif c.CommunicationMode == "Email" and c.Private.to_s == "0" %>
<%= check_box_tag 'ChkBx_Email' %>
<%= label(:lb_Email, "Email") %>
<% end %>
</td>
</tr>
<% end %>
</table>
但我得到以下错误:
undefined method `where' for #<Communication:0x4bc5490>
但是,当我正在使用以下代码:
create
@user_communication=Communication.where(:UserID => current_user.id)
if [email protected]_communication.blank?
@user_communication.each do |c|
if params[:ChkBx_Phone].to_i == 1
puts "Hassan2"
c.update_attribute(:Private, "1")
elsif params[:ChkBx_Phone].to_i == 0
puts "Ali2"
c.update_attribute(:Private, "0")
end
end
end
end
其工作正常,但它更新Private column
值Phone
和Email
,我只检查了Phone
复选框。 请建议我,等待您的回复。 谢谢。