按JOIN表中的属性或存在性查询行

时间:2016-10-18 07:48:19

标签: ruby-on-rails rails-activerecord ruby-on-rails-5

我有实体A和B(带有各自的表格)。这些实体具有N:M关系,因此有一个AB表。

A实体有一个disabled布尔字段。

我希望使用ActiveRecord从A(are not disabledare in the AB table and belong to B entity with id 1)获取所有实体。

示例:

A  
| id | name | disabled |
| a1 | foo  | false    |
| a2 | bar  | false    |
| a3 | zoo  | true     |
| a4 | hoo  | true     |

B
| id | name |
| 1  | Bob  |
| 2  | Jen  |

AB
|Aid | Bid |
| a3 | 1   |
Bob的

A个实体:foobarzoo

2 个答案:

答案 0 :(得分:1)

connect(pushButton_1x1, SIGNAL(clicked()), this, SLOT(button_1x1_clicked()));
connect(pushButton_2x2, SIGNAL(clicked()), this, SLOT(button_2x2_clicked()));


centralwidget->setLayout(horizontalLayout);
live_window->setCentralWidget(centralwidget);

live_window->setWindowTitle("Live View");
live_window->show();
}
void liveview::button_1x1_clicked()
{
qDebug() << "1 x 1";
Livecontroller->video_layout();
}
void liveview::button_2x2_clicked()
{
Livecontroller->video_layout();
}

答案 1 :(得分:1)

A.where(:disabled => false )将找到A中未被禁用的所有条目,B.find_by_id(1).as将找到A中属于B实体的所有条目,其ID为1. A.where(:disabled => false ) | B.find_by_id(1).as 2个数组提供所需的条目。

requeried_entries = A.where(:disabled => false ) | B.find_by_id(1).as