0

通过单击我的应用程序中的按钮,用户可以“邀请”另一个人加入。扫描地址簿中的电子邮件联系人,他们可以从列表中单击要向其发送电子邮件的人。

目前屏幕看起来是这样的: 在此处输入图像描述

基本上,我想要“已经被很酷的应用程序使用!” 仅当此人通讯录中的电子邮件已存在于我的数据库中时。知道怎么做吗?

用户的电子邮件在我的数据库中标识为@user.email。我知道我必须做的是'扫描我数据库中的所有@user.emails,如果它们匹配@contact.name 然后显示消息',但不知道如何编码。

我的代码的相关部分是:

<!-- go through all the contacts in a person's email address book -->
 <% @contacts.each do |name, value| %>
        <div class="email">
<!-- for all the contacts, have a check box, unchecked -->
          <%= check_box_tag "contacts[]", value, false, :id => value %>
<!-- make a label with the check box and the person's email
          <label for="<%= value %>"><%= name %></label>
          <%= "Already using my cool app!" %>
        </div>
      <% end %>

谢谢你的帮助!

4

2 回答 2

2
if !User.find_by_email(mail_address).nil?

应该做的伎俩

如果您想避免进行大量单独的数据库调用,您可以先获取所有用户的所有邮件地址的数组,然后检查它是否user.include?(mail_address)

或者

用于where(:email => array_of_contac_mail_addresses)获得有限的集合并在那里检查匹配项

于 2013-08-14T10:41:29.700 回答
1

你可以使用

User.where(:email => email).exists?

为避免多个数据库查询,您可以先获取电子邮件,例如

existing_emails = User.where(["email IN (?)", @contacts.map(&:email)]).map &:email

<% @contacts.each do |contact| %>
  ..
  <% if existing_emails.include?(contact.email) %>

  <% else %>

  <% end %>
<% end %>
于 2013-08-14T12:11:58.010 回答