Ruby on rails 列出与当前activerecord对象不相关的数据
你可以把它写在一行里Ruby on rails 列出与当前activerecord对象不相关的数据,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,你可以把它写在一行里 group = Group.find(id) // lets say group 1 // find tests that are not added to this group 首先找到属于特定组的测试ID,然后使用或也可以使用@Vasilisa查询使用连接 Test.left_outer_joins(:groups).where.not(groups: {id: id}) 一班轮 group_test_ids = group.tests.ids #test ids
group = Group.find(id) // lets say group 1
// find tests that are not added to this group
首先找到属于特定组的测试ID,然后使用或也可以使用@Vasilisa查询使用连接
Test.left_outer_joins(:groups).where.not(groups: {id: id})
一班轮
group_test_ids = group.tests.ids #test ids which belongs to group
tests = Test.where.not(id: group_test_ids) # tests which not contain that group
试试这个
group\u test\u ids=group.tests.ids
tests=test.where.not(id:group\u test\u ids)
@ts good one:)。我想我不应该给表命名group
。它与内置的活动记录冲突method@ts我把它修好后,让我试试你的解决办法,但我肯定会的
group_test_ids = group.tests.ids #test ids which belongs to group
tests = Test.where.not(id: group_test_ids) # tests which not contain that group
tests = Test.where.not(id: group.tests.ids)