简体   繁体   English

has_many通过关联

[英]has_many through associations

I'm making an online fashion store. 我正在一家网上时装商店。 I have a Category Model and a Sizes Model. 我有一个类别模型和一个大小模型。 A model can have many sizes. 一个模型可以有多种尺寸。 A Size can have many Categories. 大小可以具有许多类别。

Therefore i am using has_many through association. 因此,我通过关联使用has_many。 The category model and sizes model will be linked via a table called category_sizes. 类别模型和大小模型将通过名为category_sizes的表链接。

I will create a bunch of sizes like XS, Small, Medium. 我将创建一堆尺寸,例如XS,Small和Medium。 Then I will create a category Lets say Shirt then i can select all the Sizes a Shirt will have. 然后我将创建一个类别,假设衬衫,然后我可以选择衬衫将具有的所有尺寸。 Then click create. 然后单击创建。

How do i make the sizes appear in my view? 如何使尺寸显示在视图中? I tried for hours. 我试了几个小时。

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

Sizes 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 category_size模型

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

Here is my form. 这是我的表格。

<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.association :category_sizes %>
                <%= 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>

Schema 架构图

ActiveRecord::Schema.define(version: 20150915113019) 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

你尝试过这样的事情吗?

<%= f.select(:size_id, Size.all.map {|s| [s.title, s.id]}) %>

In your controller define , 在您的控制器中定义,

@size_variant_array = @category.sizes

In your view, 在您看来,

<%= select_tag 'size',options_for_select(@size_variant_array.collect{ |pv| [pv.title, pv.id] }) ,:prompt => "Please Select"%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM