查找其关联依次具有nil关联的所有记录

时间:2018-01-31 17:38:55

标签: ruby-on-rails activerecord

鉴于以下模型:

private static object _syncFinger = new object(); // used for syncing

private static bool AttemptIdentify()
{
    // waiting for either the user cancels or a finger is inserted
    lock (_syncFinger)
    {
        Thread tEscape = new Thread(new ThreadStart(HandleIdentifyEscape));
        Thread tIdentify = new Thread(new ThreadStart(HandleIdentify));
        tEscape.IsBackground = false;
        tIdentify.IsBackground = false;
        tEscape.Start();
        tIdentify.Start();
        Monitor.Wait(_syncFinger); // -> Wait part
    }

    // Checking the change in the locked object

    if (_syncFinger is FingerData) // checking for identity found
    {
        Console.WriteLine("Identity: {0}", ((FingerData)_syncFinger).Guid.ToString());
    }
    else if(!(_syncFinger is Char)) // char - pressed a key to return
    {
        return false; // returns with no error
    }

    return true;
}

private static void HandleIdentifyEscape()
{
    do
    {
        Console.Write("Enter 'c' to cancel: ");
    } while (Console.ReadKey().Key != ConsoleKey.C);
    _syncFinger = new Char();
    LockNotify((object)_syncFinger);
}

private static void HandleIdentify()
{
    WinBioIdentity temp = null;
    do
    {
        Console.WriteLine("Enter your finger.");
        try // trying to indentify
        {
            temp = Fingerprint.Identify(); // returns FingerData type
        }
        catch (Exception ex)
        {
            Console.WriteLine("ERROR: " + ex.Message);
        }
        // if couldn't identify, temp would stay null
        if(temp == null)
        {
            Console.Write("Invalid, ");
        }
    } while (temp == null);

    _syncFinger = temp;
    LockNotify(_syncFinger);
}

private static void LockNotify(object syncObject)
{
    lock(syncObject)
    {
        Monitor.Pulse(syncObject); 
    }
}

找到得分的最佳方法是什么,其成分有 class Score < ApplicationRecord belongs_to :composition end class Composition < ApplicationRecord has_many :scores has_one :invoice, dependent: :destroy end class Invoice < ApplicationRecord belongs_to :composition end 发票

我试过了:

nil
Score.joins(:composition).where(composition: {invoice: nil})
csn = Composition.includes(:invoice).where(invoices:{id:nil})
Score.where(csn.include? composition)
Score.where(csn.map(&:id).include? composition_id)

都有错误。有什么想法吗?

编辑:以下是Score.where(Composition.left_outer_joins(:invoice).where(invoices:{id:nil}).includes? composition) 对应的表格:

schema.rb

3 个答案:

答案 0 :(得分:1)

请尝试以下查询:

Score.joins(:composition).includes(composition: : invoice).where(invoices: { id: nil })

答案 1 :(得分:0)

尝试以下

Score.joins(:composition).where('compositions.invoice_id IS NULL')
上面应该工作,祝你好运!!!

答案 2 :(得分:0)

请尝试以下查询,

Score.joins(:composition).where('compositions.id NOT IN (?)', Invoice.pluck(:composition_id))

如果这不起作用,请告诉我compositionsscores表格中的列