ruby on rails - Polymorphic relationship inside a join table -


i'm trying create "composable" article model, user might create article add number of different "blocks", example: textblock, videoblock lastly galleryblock.

i want able this:

a = article.find(1) text = textblock.new text.content = "blah" a.blocks << text  puts a.blocks # =>[textblock, videoblock, ...] 

textblock, videoblock , galleryblock dissimilar enough using sti bad fit.

i think may possible want having join table, 1 of relationships polymorphic, haven't been able work.

here i'm at, wont function:

class article < activerecord::base   has_many :article_blocks   has_many :blocks, through: :article_blocks end  class textblock < activerecord::base   has_one :article_block, as: :block   has_one :article, through: :article_block end  class articleblock < activerecord::base   belongs_to :article   belongs_to :block, polymorphic: true end 

and schema

activerecord::schema.define(version: 20150520030333)    create_table "article_blocks", force: :cascade |t|     t.integer  "article_id"     t.integer  "block_id"     t.string   "block_type"     t.datetime "created_at", null: false     t.datetime "updated_at", null: false   end    add_index "article_blocks", ["article_id"], name: "index_article_blocks_on_article_id"   add_index "article_blocks", ["block_type", "block_id"], name: "index_article_blocks_on_block_type_and_block_id"    create_table "articles", force: :cascade |t|     t.string   "title"     t.datetime "created_at", null: false     t.datetime "updated_at", null: false   end    create_table "text_blocks", force: :cascade |t|     t.text     "content"     t.datetime "created_at", null: false     t.datetime "updated_at", null: false   end  end 

if try access articles blocks, error:

irb> x = article.new => #<article id: nil, title: nil, created_at: nil, updated_at: nil> irb> x.blocks activerecord::hasmanythroughassociationpolymorphicsourceerror: cannot have has_many :through association 'article#blocks' on polymorphic object 'block#block' without 'source_type'. try adding 'source_type: "block"' 'has_many :through' definition. 

if add source_type: "block" type error (because class doesn't exist). if change source_type: "articleblock", code doesn't fail can't insert textblocks because text blocks wrong type.

x = article.new => #<article id: nil, title: nil, created_at: nil, updated_at: nil> irb> x.blocks => #<activerecord::associations::collectionproxy []> irb> t = textblock.new => #<textblock id: nil, content: nil, created_at: nil, updated_at: nil> irb> t.content = "this text content" => "this text content" irb> x.blocks << t activerecord::associationtypemismatch: articleblock(#70362257715180) expected, got textblock(#70362257254480) 

right now, there's no clear relationship between join table , article. because it's looking section object per query, can start adjusting article class act section has_many :article_blocks, as: :section.

secondly, because have has_one relationship on textblock in relation articleblock, make sure syntax reads has_one :article_block, as: :block


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -