ruby on rails - How do I build a complex nested form to represent dynamic attributes -
i've got data model represented following image:
basically, idea there many different collections. items in single collection have same attributes, list of attributes different per collection. value of each attribute per item stored in item attribute values.
i'm trying build single page user can populate attributes item. i'm assuming nested form way go i'm @ loss how represent in controller , on page, considering names of attributes in 1 table , values in another.
if has encountered or had deal similar situation, appreciated.
thanks
here 1 potential solution.
class collection has_and_belongs_to_many :items has_and_belongs_to_many :attributes end class item has_and_belongs_to_many :collections has_many :item_attributes has_many :attributes, though: :item_attributes end class attributes has_and_belongs_to_many :collections has_many :item_attributes has_many :items, though: :item_attributes end class itemattribute belongs_to :item belongs_to :attribute end
so lets @ database layout these models:
activerecord::schema.define(version: 20151027173337) create_table "attributes_collections", id: false, force: :cascade |t| t.integer "attribute_id", null: false t.integer "collection_id", null: false end create_table "collections", force: :cascade |t| t.string "title" t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "collections", ["user_id"], name: "index_collections_on_user_id" create_table "collections_items", id: false, force: :cascade |t| t.integer "collection_id", null: false t.integer "item_id", null: false end create_table "item_attributes", force: :cascade |t| t.integer "item_id" t.integer "attribute_id" t.string "value" t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "item_attributes", ["attribute_id"], name: "index_item_attributes_on_attribute_id" add_index "item_attributes", ["item_id"], name: "index_item_attributes_on_item_id" create_table "items", force: :cascade |t| t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false end end
a better way?
but of course performance suffer due many joins. plus each attribute stored varchar means can't numeric comparisons in database.
if need flexible schema instead using hstore, json or dynamic column type or schemaless database such mongodb.
how create form / controller this?
first should acquainted can done accepts_nested_attributes_for
, fields_for
, maybe consider using ajax delegate actions out collectioncontroller
, itemcontroller
on end rather cramming single monstrosity.
Comments
Post a Comment