Ruby on rails 这其中有很多原因:联想出了问题?
预期结果:Ruby on rails 这其中有很多原因:联想出了问题?,ruby-on-rails,has-many-through,Ruby On Rails,Has Many Through,预期结果: Item.first.ratings提供该项目的评分 所需的表和列: item id,name,created_at,updated_at ratings id,name,created_at,updated_at item_ratings id,item_id,rating_id,value,created_at,updated_at 评级迁移: class CreateRatings < ActiveRecord::Migration[5.0] def c
Item.first.ratings
提供该项目的评分
所需的表和列:
item
id,name,created_at,updated_at
ratings
id,name,created_at,updated_at
item_ratings
id,item_id,rating_id,value,created_at,updated_at
评级迁移:
class CreateRatings < ActiveRecord::Migration[5.0]
def change
create_table :ratings do |t|
t.string :name
t.references :user, foreign_key: true
t.timestamps
end
end
end
class CreateJoinTableItemsRatings < ActiveRecord::Migration[5.0]
def change
create_join_table :items, :ratings do |t|
t.index [:item_id, :rating_id]
t.index [:rating_id, :item_id]
t.string :value
end
end
end
问题:
这其中有很多原因:联想出了问题?这是要问的正确问题吗?表
项目评分
不存在,但项目评分
存在,请尝试将所有出现的项目评分
重命名为项目评分
是否尝试使用重新加载来重新加载控制台代码>是的,我做了多次,也退出和返回。检查了我出错的多个教程,但似乎都没有问题。这是正确的,您应该将has\u many:item\u ratings
更改为has\u many:items\u ratings
class Item < ApplicationRecord
belongs_to :user
has_many :item_ratings
has_many :ratings, through: :item_ratings
end
class Rating < ApplicationRecord
belongs_to :user
has_many :item_ratings
has_many :items, through: :item_ratings
end
class ItemRating < ApplicationRecord
belongs_to :item
belongs_to :rating
end
Item Load (0.7ms) SELECT "items".* FROM "items" ORDER BY "items"."created_at" DESC LIMIT $1 [["LIMIT", 1]]
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "item_ratings" does not exist
LINE 8: WHERE a.attrelid = '"item_ratings"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
(SELECT c.collname FROM pg_collation c, pg_type t
WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation),
col_description(a.attrelid, a.attnum) AS comment
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"item_ratings"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum