简体   繁体   English

从Rails中的ruby表中删除记录

[英]delete record from a table in ruby on rails

I created two models "User" and "Communication", and below is the schema: 我创建了两个模型“ User”和“ Communication”,以下是架构:

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 

in User model i have has_many relation with Communication , and in Communication model i have belongs_to :user I am adding communication preferences when user sign ups to the application, and in setting, i am trying to display that user's communication preferences in separate controls. User模型中,我具有与Communication has_many关系,在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 %>

when user submits the settings page, i want to delete each record in Communication that is related to current_user and then update them from new action in controller. 当用户提交设置页面时,我想删除Communication中与current_user相关的每个记录,然后从控制器中的新操作更新它们。 But problem is that records are not deleting from Communication . 但是问题在于记录没有从Communication删除。

And below are the Controllers 下面是控制器

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

Please let me know, how i can implement this scenario, or is there any better way to update communication. 请让我知道,我如何实现此方案,或者有没有更好的方法来更新通信。 Thanks 谢谢

I think you are looking for 我想你在找

dependent: :destroy

This will make sure that all dependencies get destroyed as well. 这将确保所有依赖项也被销毁。

尝试在控制器上使用此行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM