从轨道上的ruby中的表中删除记录

时间:2014-03-05 13:26:10

标签: ruby-on-rails

我创建了两个模型" User"和"通信"以及以下是架构:

user model scheme:
id | name | Email       | SkillType     | user_Need
1  | ijk  | ijk@ijk.com | Mentor        | Ap Chinese
2  | mno  | mno@mno.com | Protege       | Ap Biology

Communication model schema:
id | UserID | CommunicationMode | CommunicationDetail
1  | 1      | Phone             | 1234567890
2  | 1      | Email             | ijk@ijk.com 
User模型中

我与has_manyCommunication个关系,在Communication模型中我有belongs_to :user 我在用户注册到应用程序时添加了通信首选项,在设置中,我试图在单独的控件中显示该用户的通信首选项。

 <%= form_for :settings,:url => {:action => :create} do |f| %>
    <div align="center" width="100%">
           <table>
            <% @user_communication.each do |user_com| %>
            <tr style="text-align: left; vertical-align: middle;">
                 <td style="font-size: large; color: #212121;">
                    Phone:
                 </td>
                 <td style="font-size: large; color: #212121;">
                    <% if user_com.CommunicationMode.to_s == "Phone" %>
                    <%= text_field_tag :phone, user_com.CommunicationDetail, placeholder: 'Phone' %>
                    <% else %>
                    <%= text_field :phone, :placeholder => "Phone" %>
                    <% end %>
                 </td>
              </tr>
              <tr style="text-align: left; vertical-align: middle;">
                 <td style="font-size: large; color: #212121;">
                    Email:
                 </td>
                 <td style="font-size: large; color: #212121;">
                    <% if user_com.CommunicationMode.to_s == "Email" %>
                    <%= text_field_tag :email, user_com.CommunicationDetail, placeholder: 'Email' %>
                    <% else %>
                    <%= text_field :email,:placeholder => "Email" %>
                    <% end %>
                 </td>
              </tr>
              <tr style="text-align: left; vertical-align: middle;">
                 <td style="font-size: large; color: #212121;">
                    Skype:
                 </td>
                 <td style="font-size: large; color: #212121;">
                    <% if user_com.CommunicationMode.to_s == "Skype" %>
                    <%= text_field_tag :skype, user_com.CommunicationDetail, placeholder: 'Skype' %>
                    <% else %>
                    <%= text_field :skype,:placeholder => "Skype" %>
                    <% end %>
                 </td>
              </tr>
              <tr style="text-align: left; vertical-align: middle;">
                 <td style="font-size: large; color: #212121;">
                    Website:
                 </td>
                 <td style="font-size: large; color: #212121;">
                    <% if user_com.CommunicationMode.to_s == "Website" %>
                    <%= text_field_tag :website, user_com.CommunicationDetail, placeholder: 'Website' %>
                    <% else %>
                    <%= text_field :website,:placeholder => "Website" %>
                    <% end %>
                 </td>
              </tr>
              <tr style="text-align: left; vertical-align: middle;">
                 <td style="font-size: large; color: #212121;">
                    Twitter:
                 </td>
                 <td style="font-size: large; color: #212121;">
                    <% if user_com.CommunicationMode.to_s == "Twitter" %>
                    <%= text_field_tag :twitter, user_com.CommunicationDetail, placeholder: 'Twitter' %>
                    <% else %>
                    <%= text_field :twitter, :placeholder => "Twitter" %>
                    <% end %>
                 </td>
              </tr>
            <% end %>
        </table>
    </div>
    <% end %>

当用户提交设置页面时,我想删除与current_user相关的通信中的每个记录,然后从控制器中的新操作更新它们。但问题是记录不会从Communication中删除。

以下是控制器

User Controller
class User < ActiveRecord::Base
  has_many :educations, dependent: :destroy
  has_many :professions, dependent: :destroy
  has_many :Communications, dependent: :destroy
  has_many :availabilities,dependent: :destroy
    before_save { self.email = email.downcase }
    before_create :create_remember_token
    validates :First_Name, presence: true,length: {maximum: 50}
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                uniqueness:  { case_sensitive: false }
    validates :password, length: { minimum: 6 }
    has_secure_password


    def User.new_remember_token
        SecureRandom.urlsafe_base64
    end

    def User.encrypt(token)
        Digest::SHA1.hexdigest(token.to_s)
    end

    private

    def create_remember_token
        self.remember_token = User.encrypt(User.new_remember_token)
    end
end
Communication Controller:
class Communication < ActiveRecord::Base
    belongs_to :user
    validates :UserID, presence: true
end

请告诉我,我如何实现此方案,或者有更好的方法来更新通信。感谢

2 个答案:

答案 0 :(得分:1)

我认为你在寻找

dependent: :destroy

这将确保所有依赖项也被销毁。

答案 1 :(得分:0)

尝试将此行用于您的控制器:

Communication.delete_all(:UserID => current_user.id)