Мой has_many: сквозная модель (Posting) не создает объект при создании первичного связанного объекта (Post).Как я могу построить отношения при создании?
Модели:
class Post
has_many :postings, dependent: :destroy
has_many :products, through: :postings, source: :postable, source_type: "Product"
has_many :Items, through: :postings, source: :postable, source_type: "Item"
end
class Posting
belongs_to :postable, polymorphic: true
belongs_to :post
end
class Product
has_many :postings, as: :postable
has_many :posts, through: :postings
end
class Item
has_many :postings, as: :postable
has_many :posts, through: :postings
end
Схема:
create_table "postings", force: :cascade do |t|
t.bigint "postable_id"
t.string "postable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "post_id"
t.index ["post_id"], name: "index_postings_on_post_id"
end
create_table "posts", force: :cascade do |t|
t.string "title"
t.string "slug"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Консоль:
Post.create(title: "Foo", product_id:3)
Traceback (most recent call last):
1: from (irb):12
ActiveModel::UnknownAttributeError (unknown attribute 'product_id' for Post.)
Цените любыесоветы!