这个has_manythrough:association哪里出错了?
Where does this has_many through: association go wrong?
期望的结果:
Item.first.ratings
给我那个项目的评分。
所需的 table 和列:
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
加入table迁移:
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
物品型号:
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
item_rating 型号:
class ItemRating < ApplicationRecord
belongs_to :item
belongs_to :rating
end
rails console
Item.first.ratings
结果:
Item Load (0.7ms) SELECT "items".* FROM "items" ORDER BY "items"."created_at" DESC LIMIT [["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
问题:
这个has_manythrough:association哪里出错了?这是正确的问题吗?
Table item_ratings
不存在但 items_ratings
存在,尝试将所有出现的 item_ratings
重命名为 items_ratings
.
期望的结果:
Item.first.ratings
给我那个项目的评分。
所需的 table 和列:
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
加入table迁移:
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
物品型号:
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
item_rating 型号:
class ItemRating < ApplicationRecord
belongs_to :item
belongs_to :rating
end
rails console
Item.first.ratings
结果:
Item Load (0.7ms) SELECT "items".* FROM "items" ORDER BY "items"."created_at" DESC LIMIT [["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
问题: 这个has_manythrough:association哪里出错了?这是正确的问题吗?
Table item_ratings
不存在但 items_ratings
存在,尝试将所有出现的 item_ratings
重命名为 items_ratings
.