In my communication table I have some columns:
id | UserID | CommunicationMode | CommunicationDetail | Private
1 | 1 | Phone | 123456789 | 1
2 | 1 | Email | abc@abc.com | 1
And I want to update column value using where clause using loop like below:
create
@user_communication=Communication.where(:UserID => current_user.id)
if !@user_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
I want to check above that if Phone
checkbox is checked then it updates Private
column with value 1
else 0
where CommunicationMode is Phone
and for email I want to check that if Email
checkbox is checked then it updates Private
column with value 1
else 0
where CommunicationMode is Email
And below is Phone
and Email
checkboxes:
<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>
But I am getting an error below:
undefined method `where' for #<Communication:0x4bc5490>
But when I am using below code:
create
@user_communication=Communication.where(:UserID => current_user.id)
if !@user_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
Its working fine but it update both Private column
value of Phone
and Email
and I have checked only Phone
checkbox.
Kindly suggest me, waiting for your reply.
Thanks.