:wishes, :source => :user -6ren">
gpt4 book ai didi

ruby-on-rails - Rails 3错误:对象不支持包含#inspect的检查(左外部联接)

转载 作者:行者123 更新时间:2023-12-04 05:59:02 27 4
gpt4 key购买 nike

我正在使用Rails 3。

我有3种型号:

class Deal < ActiveRecord::Base
has_many :wishes, :class_name => "Wish"
has_many :wishers, :through => :wishes, :source => :user
end

class User < ActiveRecord::Base
has_many :wishes, :class_name => "Wish", :conditions => { 'wishes.wished' => true }
has_many :wished_deals, :through => :wishes, :source => :deal
end

class Wish < ActiveRecord::Base
belongs_to :user
belongs_to :deal
end


我正在尝试在Deal模型中创建以下范围:

scope :not_wished_by_user, lambda { |user| includes(:wishes).where('wishes.wished' != true, 'wishes.user_id' => user) }


我想要的是所有交易,除了区块中给定用户将其标记为“交易”的交易。但是每当我这样做时,都会出现以下错误:

ruby-1.9.2-head > u = User.all.first
ruby-1.9.2-head > Deal.not_wished_by_user(u)
(Object doesn't support #inspect)
=>


同样,将其放在函数中也不起作用。知道这可能是什么吗?

谢谢!

编辑:这些是愿望表迁移

class CreateWish < ActiveRecord::Migration
def self.up
create_table :wishes do |t|
t.integer :deal_id
t.integer :user_id
t.boolean :wished, :default => true
t.boolean :collected, :default => false
t.datetime :collected_date
t.timestamps
end

add_index :wishes, [:deal_id, :user_id], :uniq => true
end
end

最佳答案

请参见下面的更新vv

旧答案

您没有使用任何Deal属性进行选择,因此请尝试将代码移入Wish类:

class Wish < ActiveRecord::Base
belongs_to :user
belongs_to :deal

scope :'wished?', lambda{ |f| where('wished = ?', f) }

scope :not_wished_by_user, lambda{|user| wished?(false).where('user_id = ?', user)}
end


用法示例和输出:

ruby-1.9.2-p180 :023 > Wish.not_wished_by_user(User.first).to_sql
=> "SELECT \"wishes\".* FROM \"wishes\" WHERE (wished = 't') AND (user_id = 1)"


这对您来说是正确的结果吗?

PS:

Deal中,您可以保留代理方法,例如:

class Deal < ActiveRecord::Base
has_many :wishes, :class_name => "Wish"
has_many :wishers, :through => :wishes, :source => :user

def self.not_wished_by_user(user)
Wish.not_wished_by_user(user)
end
end


Update1(子查询)

class Deal < ActiveRecord::Base
has_many :wishes, :class_name => "Wish"
has_many :wishers, :through => :wishes, :source => :user

scope :deal_ids_not_wished_by_user, lambda { |user|
joins(:wishes).where('wishes.user_id = ?', user).where('wishes.wished = ?', false).select('deals.id')
}

scope :wished_by_user, lambda { |user|
where("id not in (#{Deal.deal_ids_not_wished_by_user(user).to_sql})")
}
end


用法示例和输出:

ruby-1.9.2-p180 :023 > Deal.wished_by_user(User.first).to_sql
=> "SELECT \"deals\".* FROM \"deals\" WHERE (id not in (SELECT deals.id FROM \"deals\" INNER JOIN \"wishes\" ON \"wishes\".\"deal_id\" = \"deals\".\"id\" WHERE (wishes.user_id = 1) AND (wishes.wished = 'f')))"


UPD2(泛滥的外部联接)

交易类别:

class Deal < ActiveRecord::Base
has_many :wishes, :class_name => "Wish"
has_many :wishers, :through => :wishes, :source => :user

scope :not_wished_excluded, lambda { |user|
joins('LEFT OUTER JOIN wishes on wishes.deal_id = deals.id').
where('wishes.user_id = ? OR wishes.user_id is null', user).
where('wishes.wished = ? OR wishes.wished is null', true)
}
end


用法:

ruby-1.9.2-p180 :096 > Deal.not_wished_excluded(User.first).to_sql
=> "SELECT \"deals\".* FROM \"deals\" LEFT OUTER JOIN wishes on wishes.deal_id = deals.id WHERE (wishes.user_id = 1 OR wishes.user_id is null) AND (wishes.wished = 't' OR wishes.wished is null)"

关于ruby-on-rails - Rails 3错误:对象不支持包含#inspect的检查(左外部联接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5813855/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com