如何使用连接或其他方式让我使用 where 查询来比较另一个模型中的其他条件?
模型关系
class SonyAlarmLog < ActiveRecord::Base
belongs_to :sony_alarm_test
class SonyAlarmTest < ActiveRecord::Base
has_many :sony_alarm_bugs, :dependent => :destroy
它有效,但返回我的列SonyAlarmTest
,我想要的列sony_alarm_logs
SonyAlarmTest.joins{:sony_alarm_logs}
.where{ sony_alarm_logs.name =~ event_name }
它失败了,得到ActiveRecord::ConfigurationError: Association named 'sony_alarm_tests' was not found on SonyAlarmLog; perhaps you misspelled it?
错误
SonyAlarmLog.joins(:sony_alarm_tests)
.where{ name =~ event_name }
更新(修复:它应该使用单数名称)
但是还是ActiveModel::MissingAttributeError: missing attribute: firmware_version
报错
我认为这可能是滥用 squeel 语法?
SonyAlarmLog.joins(:sony_alarm_test)
.where{ name =~ event_name }
.where{ utc_time.gt (my{utc_time} + TIME_CORRECTION) }
.where{ sony_alarm_tests.round.eq(my{sony_alarm_test.round}) }
.where{ sony_alarm_tests.ip =~ my{sony_alarm_test.ip} }
.where{ sony_alarm_tests.firmware_version =~ my{sony_alarm_test.firmware_version} }