У меня есть базовый класс Place
и несколько подклассов, использующих соглашения STI.У меня есть отдельная модель Post
, которая belongs_to
один из подклассов Place
:
class Place < ApplicationRecord
end
class SubPlace < Place
has_many :posts, class_name: "SubPlace", foreign_key: "sub_place_id"
end
class Post < ApplicationRecord
belongs_to :sub_place, class_name: "SubPlace", foreign_key: "sub_place_id"
end
Можно сохранить новую запись Post
с помощью консоли Rails, но яполучить следующую ошибку при попытке найти Posts
для определенного SubPlace
:
ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column places.sub_place_id does not exist)
Есть ли способ выполнить эту работу, или мои ассоциации должны относиться только к базовому классу?
Добавлена схема:
create_table "posts", force: :cascade do |t|
t.string "title"
t.bigint "sub_place_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["sub_place_id"], name: "index_posts_on_sub_place_id"
end
create_table "places", force: :cascade do |t|
t.string "name"
t.string "type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end