Rails 多态连接 has_many,通过 returns 不可能 SQL
Rails polymorphic join has_many, through returns impossible SQL
我有3个模型设置如下:
class User < ActiveRecord::Base
has_many :interests, as: :interesting, dependent: :destroy
has_many :games, through: :interests, source: :interesting, source_type: 'Game'
has_many :people, through: :interests, source: :interesting, source_type: 'Person'
end
class Interest < ActiveRecord::Base
belongs_to :interesting, polymorphic: true
validates :user_id, presence: true
validates :interesting_id, presence: true
end
class Game < ActiveRecord::Base
has_many :users, through: :interests
has_many :interests, as: :interesting
end
class Person < ActiveRecord::Base
has_many :users, through: :interests
has_many :interests, as: :interesting
end
当我尝试调用 user.games
时,数据库中的 SQL 运行 是
SELECT "games".* FROM "games"
INNER JOIN "interests"
ON "game"."id" = "interests"."interesting_id"
WHERE "interests"."interesting_id" = AND
"interests"."interesting_type" = AND
"interests"."interesting_type" =
[["interesting_id", 3],
["interesting_type", "User"],
["interesting_type", "Game"]]
所以显然没有返回任何内容。只要不包含 ["interesting_type", "User"]
,查询就应该有效。
我做错了什么?设置 User
class 以及 Game
和 Person
class 的最佳方法是什么?
我正在使用 Rails v4.2.6
请试试这个
class User < ActiveRecord::Base
has_many :interests, dependent: :destroy
has_many :games, as: :interesting, through: :interests, source_type: 'Game'
has_many :people, as: :interesting, through: :interests, source_type: 'Person'
end
class Interest < ActiveRecord::Base
belongs_to :interesting, polymorphic: true
validates :user_id, presence: true # I don't know the reason to use that if you use as polymorphic
validates :interesting_id, presence: true
end
table_name interests
它应该只有属性 :id:, :interesting_id, :interesting_type
总而言之,以下似乎适用于此用例:
User < ActiveRecord::Base
has_many :interests, dependent: :destroy
has_many :games, through: :interests,
source: :interesting, source_type: 'Game'
has_many :people, through: :interests,
source: :interesting, source_type: 'Person'
end
class Interest < ActiveRecord::Base
belongs_to :user
belongs_to :interesting, polymorphic: true
validates :user_id, presence: true
validates :interesting_id, presence: true
end
我有3个模型设置如下:
class User < ActiveRecord::Base
has_many :interests, as: :interesting, dependent: :destroy
has_many :games, through: :interests, source: :interesting, source_type: 'Game'
has_many :people, through: :interests, source: :interesting, source_type: 'Person'
end
class Interest < ActiveRecord::Base
belongs_to :interesting, polymorphic: true
validates :user_id, presence: true
validates :interesting_id, presence: true
end
class Game < ActiveRecord::Base
has_many :users, through: :interests
has_many :interests, as: :interesting
end
class Person < ActiveRecord::Base
has_many :users, through: :interests
has_many :interests, as: :interesting
end
当我尝试调用 user.games
时,数据库中的 SQL 运行 是
SELECT "games".* FROM "games"
INNER JOIN "interests"
ON "game"."id" = "interests"."interesting_id"
WHERE "interests"."interesting_id" = AND
"interests"."interesting_type" = AND
"interests"."interesting_type" =
[["interesting_id", 3],
["interesting_type", "User"],
["interesting_type", "Game"]]
所以显然没有返回任何内容。只要不包含 ["interesting_type", "User"]
,查询就应该有效。
我做错了什么?设置 User
class 以及 Game
和 Person
class 的最佳方法是什么?
我正在使用 Rails v4.2.6
请试试这个
class User < ActiveRecord::Base
has_many :interests, dependent: :destroy
has_many :games, as: :interesting, through: :interests, source_type: 'Game'
has_many :people, as: :interesting, through: :interests, source_type: 'Person'
end
class Interest < ActiveRecord::Base
belongs_to :interesting, polymorphic: true
validates :user_id, presence: true # I don't know the reason to use that if you use as polymorphic
validates :interesting_id, presence: true
end
table_name interests
它应该只有属性 :id:, :interesting_id, :interesting_type
总而言之,以下似乎适用于此用例:
User < ActiveRecord::Base
has_many :interests, dependent: :destroy
has_many :games, through: :interests,
source: :interesting, source_type: 'Game'
has_many :people, through: :interests,
source: :interesting, source_type: 'Person'
end
class Interest < ActiveRecord::Base
belongs_to :user
belongs_to :interesting, polymorphic: true
validates :user_id, presence: true
validates :interesting_id, presence: true
end