从远程关联构建ActiveRecord查询

时间:2015-09-01 18:50:54

标签: ruby-on-rails activerecord

如果Diary有多个Page s,

并且每个Page都有许多LoveLetter s,

LoveLetter有许多RecipientsRelationships

然后使用ActiveRecord,如何选择PageLoveLetter s Recipient s Relationship s name == "Bob"的所有where

我尝试使用diary = Diary.take diary.pages.where(love_letters: { recipient_id #... # I can't put 'recipient' here and keep nesting hashes, apparently. # I think I have to specify the column name. ,但它让我在一定数量的嵌套关联后指定了一个外键,我不能再查询模型了:

# coding=utf-8
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.factory import Factory
from kivy.storage.jsonstore import JsonStore
from suds.client import Client
import sys
import os
import time, threading


class LoginForm(BoxLayout):
    login_input = ObjectProperty()
    password_input = ObjectProperty()

    def log_in(self):
        self.clear_widgets()
        menu = Factory.MenuWindow()
        self.add_widget(menu)
        MenuWindow.change_caption(MenuWindow)

    def show_settings(self):
        self.clear_widgets()
        pokaz = Factory.SettingsWindow()
        self.add_widget(pokaz)


def set_written_data_ip():
    store = JsonStore('anakonda_mobile.json')
    if store.exists('Adres'):
        print(store.get('Adres'))
        if store.get('Adres')['ip'].strip() != '':
            return store.get('Adres')['ip']
    else:
        return ''


def set_written_data_port():
    store = JsonStore('anakonda_mobile.json')
    if store.exists('Adres'):
        if store.get('Adres')['port'].strip() != '':
            return store.get('Adres')['port']
    else:
        return ''


class SettingsWindow(BoxLayout):
    ip_value = ObjectProperty(set_written_data_ip())
    port_value = ObjectProperty(set_written_data_port())

    def show_logging(self):
        self.clear_widgets()
        self.add_widget(LoginForm())

    def save_ip_port(self):
        store = JsonStore('anakonda_mobile.json')
        store.put('Adres', ip=self.ip_input.text, port=self.port_input.text)
        python = sys.executable
        os.execl(python, python, *sys.argv)


class MenuWindow(BoxLayout):
    def soap_connect(self):
        try:
            self.a = Client('http://192.168.1.1:7789/ASOAPService?wsdl')
        except Exception as g:
        return ''

    def change_caption(self):
        self.soap_connect(self)
        return 'SOAP connected'


class MobileApp(App):
    pass


if __name__ == '__main__':
    MobileApp().run()

1 个答案:

答案 0 :(得分:2)

不确定这是否有效但不在我的头顶:

 Page.joins(:love_letters).merge(LoveLetter.joins(:recipients)).merge(Recipient.where(:name => "Bob"))

如果这不起作用,请尝试合并关系。

Page.joins(:love_letters).merge(LoveLetter.joins(:recipients)).merge(Relationship.merge(:recipients)).merge(Recipient.where(:name => "Bob"))