我正在尝试查询以下
Vehicle.where(:account.name => "").count
我得到了这个错误:
NoMethodError: undefined method `name' for :account:Symbol
车辆和帐户之间的关联已在模型上定义,我的用户约定。为什么我会遇到此错误?
车辆。RB
class Vehicle < ActiveRecord::Base
belongs_to :account
end
account.rb
class Account < ActiveRecord::Base
has_many :vehicles
end
模式
create_table "accounts", force: true do |t|
t.string "name"
...
end
create_table "vehicles", force: true do |t|
t.integer "account_id"
...
end
您可以使用以下内容:
Vehicle.joins(:account).where("accounts.name = ?", "").count
您也可以使用以下方式搜索空白字段
Vehicle.joins(:account).where("accounts.name <> ''").count
错误正在加剧,因为铁轨将account
视为列,如果您想到达关联表的条件,请在它们之间进行加入,例如plast
Vehicle.joins('account').where("account.name IS NULL").count