Rails依赖销毁不工作

时间:2018-06-13 09:55:26

标签: ruby-on-rails postgresql

我有kubectlUser型号。 Organization Userhas_many :organizations Organization

当我想使用has_many :users从db销毁用户时,我收到错误@user.destroy

以下是我的用户和组织模型:

Organization.rb

Key (id)=(3) is still referenced from table "organizations".

User.rb

class Organization < ApplicationRecord
  extend FriendlyId
  friendly_id :name, :use => :slugged

  has_many :members, :dependent => :destroy
  has_many :users, :through => :members, :dependent => :destroy
  has_many :moderators, -> { where :members => { :role => 1 } }, :through => :members, :source => :user
  has_many :admins, -> { where :members => { :role => 2 } }, :through => :members, :source => :user
  has_many :campains, :dependent => :destroy
  has_many :statuses, :as => :statusable
  has_many :activities
  has_many :world_members
  has_many :teams

  accepts_nested_attributes_for :members, :users

Member.rb

class User < ApplicationRecord
  extend FriendlyId
  friendly_id :full_name, :use => :slugged
  acts_as_voter

  enum role: [:user, :moderator, :organization, :admin]

  has_many :members, :class_name => "Member", :foreign_key => "user_id", :dependent => :destroy
  has_many :organizations, :through => :members, :dependent => :destroy
  has_many :conversations, :foreign_key => :sender_id

  has_many :admin_organizations, ->{ where(members: {role: 2}) }, :through => :members, source: :organization
  has_many :moderate_organizations, ->{ where(members: {role: 1}) }, :through => :members, source: :organization
  has_many :member_organizations, ->{ where(members: {role: 0}) }, :through => :members, source: :organization

  accepts_nested_attributes_for :members, :organizations

1 个答案:

答案 0 :(得分:0)

对于dependent_destroy:organizations:users:members不应该class User < ActiveRecord::Base has_many :members, dependent: :destroy has_many :organizations, through: :members end class Organization < ActiveRecord::Base has_many :members, dependent: :destroy has_many :users, through: :members end class Member < ActiveRecord::Base belongs_to :user belongs_to :organization end

您没有从组织到用户,也没有从用户到组织的直接关系。

using System;
using System.IO;
using System.Collections.Generic;
using System.Media;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Win32;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;

namespace Player
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
        float volume1,volume2;
        private WaveOutEvent player1 = null;
        private WaveOutEvent player2 = null;
        private AudioFileReader audioFileReader1 = null;
        private AudioFileReader audioFileReader2 = null;
        private bands = new

        public Window1()
        {
            InitializeComponent();
            dispatcherTimer.Tick += dispatcherTimer_Tick;
            dispatcherTimer.Interval = new TimeSpan(0,0,1);
            dispatcherTimer.Start();
            player1 = new WaveOutEvent();
            player2 = new WaveOutEvent();
        }
        void progressBar1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {

        }

        void browse1_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "MP3 File (.mp3) | *.mp3;";
            if(openFileDialog.ShowDialog() == true){
                audioFileReader1 = new AudioFileReader(openFileDialog.FileName);
                player1.Init(audioFileReader1);
                title1.Text = audioFileReader1.FileName;
            }
        }

        void browse2_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "MP3 File (.mp3) | *.mp3;";
            if(openFileDialog.ShowDialog() == true){
                audioFileReader2 = new AudioFileReader(openFileDialog.FileName);
                player2.Init(audioFileReader2);
                title2.Text = audioFileReader2.FileName;
            }
        }

        void play1_Click(object sender, RoutedEventArgs e)
        {
            volume_set1();
            player1.Play();
        }

        void pause1_click(object sender, RoutedEventArgs e)
        {
            player1.Stop();
        }

        void pause2_Click(object sender, RoutedEventArgs e)
        {
            player2.Stop();
        }

        void play2_click(object sender, RoutedEventArgs e)
        {
            volume_set2();
            player2.Play();
        }

        void progress2_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            progress2.Maximum = (int)(audioFileReader2.TotalTime.TotalSeconds);
            progress2.Value   = (int)(audioFileReader2.CurrentTime.TotalSeconds);
        }

        void progress1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            progress1.Maximum = (int)(audioFileReader1.TotalTime.TotalSeconds);
            progress1.Value   = (int)(audioFileReader1.CurrentTime.TotalSeconds);
        }

        void crossfader_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            volume_set2();
            volume_set1();
        }

        void overall_volume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            volume_set2();
            volume_set1();
        }

        void volume1_Control_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            volume_set1();
        }

        void volume2_Control_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            volume_set2();
        }

        void stop1_Click(object sender, RoutedEventArgs e)
        {
            player1.Stop();
            player1.
        }

        void stop2_Click(object sender, RoutedEventArgs e)
        {
            player2.Stop();
        }

        void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            if(audioFileReader1 != null){
                progress1.Value   = (int)(audioFileReader1.CurrentTime.TotalSeconds);
            }
            if(audioFileReader2 != null){
                progress2.Value   = (int)(audioFileReader2.CurrentTime.TotalSeconds);
            }
        }

        void volume_set1()
        {
            if(audioFileReader1 != null){
                volume1 = (float)(volume1_Control.Value * overall_volume.Value * (0.5 - (crossfader.Value * 0.5)));
                audioFileReader1.Volume = volume1;
            }
        }
        void volume_set2()
        {
            if(audioFileReader2 != null){
                volume2 = (float)(volume2_Control.Value * overall_volume.Value * (0.5 + (crossfader.Value * 0.5)));
                audioFileReader2.Volume = volume2;
            }
        }
        }
} 

由于您不想删除组织中的所有用户,因此只要引用组织,如果删除用户,则不要删除用户所连接的所有组织,只需参考