gpt4 book ai didi

ruby-on-rails-3 - Postgresql 使用 Rails 3 寻找错误的多对多表

转载 作者:行者123 更新时间:2023-11-29 11:43:59 25 4
gpt4 key购买 nike

**更新:我发现了这个:

“Active Record 期望中间连接表以其连接的表的串联命名,并按字母顺序排列。”

唉……**

我有 3 个模型:图像、视频和关键字。图片和视频看起来基本相同,我一直在尝试在关键字方面为它们做所有相同的事情。但是,向图像添加关键字效果很好,而向视频添加关键字则不行。

关键词.rb

has_and_belongs_to_many :cases
has_and_belongs_to_many :images
has_and_belongs_to_many :videos

视频.rb

has_and_belongs_to_many :keywords

图片.rb

has_and_belongs_to_many :keywords

架构.rb

create_table "images_keywords", :id => false, :force => true do |t|
t.integer "image_id"
t.integer "keyword_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "videos_keywords", :id => false, :force => true do |t|
t.integer "video_id"
t.integer "keyword_id"
t.datetime "created_at"
t.datetime "updated_at"
end

这个有效:

图像 Controller .rb

@image.keywords << Keyword.new(:word => "test")

这个:

videos_controller.rb

@video.keywords << Keyword.new(:word => "test")

给我这个错误:

ActiveRecord::StatementInvalid in VideosController#create

PGError: ERROR: relation "keywords_videos" does not exist
LINE 4: WHERE a.attrelid = '"keywords_videos"'::regclas...
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"keywords_videos"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum

当我检查控制台以将关键字添加到图像时,它会正确查找“images_keywords” 为什么它会为视频查找“keywords_videos”???

两者有什么区别?

最佳答案

当使用has_and_belongs_to_many时,连接表的名称需要有相关模型按字母顺序列出,即:keywords_videos 而不是videos_keywords .

关于ruby-on-rails-3 - Postgresql 使用 Rails 3 寻找错误的多对多表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6147515/

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