为什么您可以通过包含“行对象”数组的变量传递。到。

发布于 2025-02-12 09:17:56 字数 1618 浏览 0 评论 0原文

我目前正在学习Activerecord并在RSPEC中进行一些测试训练。
下面的测试的缩写版本在下面。

我可以通过更改结果变量为结果= row.where.not(id:rows_not_included)来使其通过,但是我不明白为什么您可以't通过将行对象保存到.Excluding方法的变量,并获得预期的结果,而无需特别引用列
我是错误地考虑“行对象”,还是我不了解的是其他东西?为什么您需要将特定列引用以读取此参数的方式?为什么Rails可以理解一个实例变量指向表中的行的数组变量是什么是在期望内的,但是当它通过作为ActivereCord查询的参数传递时不是

导轨v:5.2.4

    @row_1 = Row.create(:blah, :blah_blah)
    @row_2 = Row.create(:blah, :blah_blah)
    @row_3 = Row.create(:blah, :blah_blah)
    @row_4 = Row.create(:blah, :blah_blah)

    rows_not_included = [@row_1, @row_3]
    expected_result = [@row_2, @row_4]

    results = Row.excluding(rows_not_included)
    # ----->  Row.where.not(rows_not_included) produces the same results; see below
    expect(results).to eq(expected_result)

输出错误

[1] pry(#<RSpec::ExampleGroups::ActiveRecordObstacleCourseWeek2>)> Item.where.not(items_not_included)NoMethodError: undefined method `include?' for #<Item:0x00007fa61caa1e90>
from /Users/brennastuart/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/activemodel-5.2.8/lib/active_model/attribute_methods.rb:430:in `method_missing'

[4] pry(#<RSpec::ExampleGroups::ActiveRecord>)> Item.excluding(items_not_included)
NoMethodError: undefined method `excluding' for #<Class:0x00007fcab2853ea8>
from /Users/brennas/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/activerecord-5.2.8/lib/active_record/dynamic_matchers.rb:22:in `method_missing'

I'm currently learning ActiveRecord and working through some testing drills in RSpec.
An abbreviated version of the test that is tripping me up is below.

I can make it pass by changing the results variable to results = Row.where.not(id: rows_not_included), but I can't understand why you can't pass the variable that holds the row objects to the .excluding method and get the expected results without specifically referencing a column?
Am I thinking about "row objects" incorrectly, or is there something more that I don't understand? Why do you need to reference a specific column for this argument to be read the way it is intended? Why can Rails understand what an array of instance variables that points to rows in a table is when it is inside of an expectation, but not when it is passed as an argument for an ActiveRecord query?

Rails v: 5.2.4

    @row_1 = Row.create(:blah, :blah_blah)
    @row_2 = Row.create(:blah, :blah_blah)
    @row_3 = Row.create(:blah, :blah_blah)
    @row_4 = Row.create(:blah, :blah_blah)

    rows_not_included = [@row_1, @row_3]
    expected_result = [@row_2, @row_4]

    results = Row.excluding(rows_not_included)
    # ----->  Row.where.not(rows_not_included) produces the same results; see below
    expect(results).to eq(expected_result)

Output error for .where.not:

[1] pry(#<RSpec::ExampleGroups::ActiveRecordObstacleCourseWeek2>)> Item.where.not(items_not_included)NoMethodError: undefined method `include?' for #<Item:0x00007fa61caa1e90>
from /Users/brennastuart/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/activemodel-5.2.8/lib/active_model/attribute_methods.rb:430:in `method_missing'

Output error for .excluding:

[4] pry(#<RSpec::ExampleGroups::ActiveRecord>)> Item.excluding(items_not_included)
NoMethodError: undefined method `excluding' for #<Class:0x00007fcab2853ea8>
from /Users/brennas/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/activerecord-5.2.8/lib/active_record/dynamic_matchers.rb:22:in `method_missing'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╄→承喏 2025-02-19 09:17:56

您说当您分配这样的结果时,测试正在通过吗?

results = Row.where.not(id: rows_not_included)

上面的查询将搜索其id与给出的对象的ID不同的行。如果指定列名,则Rails将尝试查询其中包括您指定的列的条件,并将其与给定对象的ID属性进行比较。我只是尝试了一些方案,并发现了这一点。

results = Row.where.not(name: rows_not_included)

上面的代码将将数据库中的行记录的名称列与ROWS_NOT_INCLUDED对象的IDS进行比较。

You said test is passing when you assign results like this right?

results = Row.where.not(id: rows_not_included)

The query above will search rows that its id different from the ids of objects that given as parameter. If you specify a column name then rails will try to query with where condition that includes the column you specified and compare it with the id attributes of the given objects. I just tried some scenarios and found out this.

results = Row.where.not(name: rows_not_included)

The above code will compare the name column of the row records in database with ids of the rows_not_included objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文