简体   繁体   中英

delete record from a table in ruby on rails

I created two models "User" and "Communication", and below is the schema:

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.

 <%= 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. But problem is that records are not deleting from 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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