gpt4 book ai didi

ruby-on-rails - Rails 4 中连接表的默认范围

转载 作者:行者123 更新时间:2023-12-02 07:01:14 25 4
gpt4 key购买 nike

我必须只选择属于 ctype = TV channel 的节目时间表

但我收到此错误:

enter image description here

程序 (模型)

class Program < ActiveRecord::Base
has_many :program_schedules, dependent: :delete_all

节目表 (模型)
class ProgramSchedule < ActiveRecord::Base
belongs_to :program
belongs_to :channel


default_scope { includes(:channel).where("program_schedules.channels.ctype NOT IN (?)", ['tv']) }

end

channel (模型)
class Channel < ActiveRecord::Base

validates :name, :site, :ctype, :country, :presence => true

has_many :programs, :dependent => :delete_all

查询
@programs = Program.includes(:program_schedules).where(
"programs.ptype" => ["tv_show","movie"],
"programs.country" => @country).
select("programs.id").
group("programs.id, program_schedules.id, channels.id").
having("program_schedules.stop > ?", Time.current)

架构.rb
  create_table "channels", force: true do |t|
t.string "site"
t.string "name"
t.string "icon"
t.string "ctype"
t.string "country"
t.string "lang"
t.integer "ordertab"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "homepage"
end

create_table "programs", force: true do |t|
t.string "country"
t.string "title"
t.text "subtitle"
t.text "description"
t.float "official_rating", default: 0.0
t.float "rating", default: 0.0
t.string "ptype"
t.string "year"
t.string "imdb"
t.string "tmdb"
t.string "tvdb"
t.string "wiki"
t.string "poster"
t.string "backdrop"
t.string "trailer"
t.float "popularity", default: 0.0
end

create_table "program_schedules", force: true do |t|
t.integer "program_id"
t.datetime "start"
t.datetime "stop"
t.integer "channel_id"
end

最佳答案

您的 includes应该包括关联而不是符号形式的表名。尝试将默认范围更改为以下内容:

default_scope { includes(:channel).where("channels.ctype NOT IN (?)", ['tv']) }

关于ruby-on-rails - Rails 4 中连接表的默认范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20363044/

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