mercredi 23 septembre 2015

Has_man through: association for category_sizes

Hello i'm creating a clothing store. I have Categories that have Sizes. A Womens Shirt(category) might have XS, S, M, Large. A mens shirt can have XS, S, M, L. Shoes can have 4-16 and so on.

I have created a has_many through: association that connects the Category table with Sizes table by a Cateogry_Sizes table.

When admin creates a Category they should select all the sizes that category will need.

How can i select the sizes in the below view. The current code is incorrect? In console When i go category.sizes i just get an empty array

view

<div class="container">
  <div class=“row”>
    <div class="col-md-6 col-md-offset-3">
      <div class="panel panel-primary">
        <div class="panel-body">
                    <%= simple_form_for(@category) do |f| %>
                      <div class="form-inputs">
                        <%= f.input :name %>
              <%= f.select(:sizes, Size.all.map {|s| [s.title, s.id]}, :multiple => true) %>
              <%= f.collection_select :parent_id, Category.order(:name), :id, :name, {prompt: "Select Parrent ID If Applicable"},include_blank: true %>
              <div class="form-actions"><%= f.button :submit %></div>
            </div>
          <% end %>
        </div>
      </div>
    </div>
  </div>
</div>

Category model.

class Category < ActiveRecord::Base
    has_ancestry
    has_many :items
    validates :name, presence: true, length: { maximum: 20 }
  has_many :category_sizes
  has_many :sizes, through: :category_sizes
end

Size model

class Size < ActiveRecord::Base
    validates :title, presence: true, length: { maximum: 15 }
    validates :title, uniqueness: true

  has_many :category_sizes
  has_many :categories, through: :category_sizes
end

Category_size model

class CategorySize < ActiveRecord::Base
  belongs_to :category
  belongs_to :size
end

Schema

ActiveRecord::Schema.define(version: 20150920013947) do

  create_table "categories", force: :cascade do |t|
    t.string   "name"
    t.string   "ancestry"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "categories", ["ancestry"], name: "index_categories_on_ancestry"

  create_table "category_sizes", force: :cascade do |t|
    t.integer  "category_id"
    t.integer  "size_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  add_index "category_sizes", ["category_id"], name: "index_category_sizes_on_category_id"
  add_index "category_sizes", ["size_id"], name: "index_category_sizes_on_size_id"

  create_table "items", force: :cascade do |t|
    t.string   "title"
    t.decimal  "price"
    t.text     "description"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.integer  "user_id"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.integer  "category_id"
  end

  add_index "items", ["user_id", "created_at"], name: "index_items_on_user_id_and_created_at"
  add_index "items", ["user_id"], name: "index_items_on_user_id"

  create_table "sizes", force: :cascade do |t|
    t.text     "title"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "taggings", force: :cascade do |t|
    t.integer  "tag_id"
    t.integer  "taggable_id"
    t.string   "taggable_type"
    t.integer  "tagger_id"
    t.string   "tagger_type"
    t.string   "context",       limit: 128
    t.datetime "created_at"
  end

  add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
  add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"

  create_table "tags", force: :cascade do |t|
    t.string  "name"
    t.integer "taggings_count", default: 0
  end

  add_index "tags", ["name"], name: "index_tags_on_name", unique: true

  create_table "users", force: :cascade do |t|
    t.string   "username"
    t.string   "email"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "password_digest"
    t.string   "remember_digest"
    t.boolean  "admin",               default: false
    t.string   "activation_digest"
    t.boolean  "activated",           default: false
    t.datetime "activated_at"
    t.string   "reset_digest"
    t.string   ">"
    t.datetime "reset_sent_at"
    t.string   "avatar_file_name"
    t.string   "avatar_content_type"
    t.integer  "avatar_file_size"
    t.datetime "avatar_updated_at"
    t.text     "description"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true

end

Aucun commentaire:

Enregistrer un commentaire