0

我將如何處理belongs_to以下include。而不是顯示product_colour_id id喜歡顯示關聯的顏色(更新:解決此部分下面)。 product_colour_id位於Product表中,並與相應的Product_colour標識相匹配。Rails 4包含多個has_many和belongs_to index.html.erb

它是兩個或更多has_many關聯,我無法解決的情況。可以做到嗎?

應用程序/控制器/ home_controller.rb

class HomeController < ApplicationController 
    def index 
    products = Product.last(5) 
    product_ids = products.map(&:id) 
    @product_colour_ids = products.map(&:product_colour_id) 
    @allproduct_colours = ProductColour.all 
    @product_colour_map = ProductColour.find(@product_colour_ids) 
    @product_images = Product.includes(:product_images) 
        .where(product_images: {product_id: product_ids, :default_image => true}) 
    end 
end 

/app/views/home/index.html.erb

<% @product_images.each do |pd| %> 
      <%= content_tag :div, :class => "col-md-3 col-sm-6 hero-feature" do %> 
      <% pd.product_images.each do |i| %> 
       <div class="thumbnail"> 
       <%= image_tag (i.product_image(:medium)) %> 
      <% end %> </div> 
      <div class="caption"> 
      <h3><%= pd.product_name %></h3> 
      <p><%= pd.product_description %></p> 
      <p> <%= pd.product_colour_id %></p> 
      </div> 
      <% end %> 
     <% end %> 
    </div> 

我難以找到實施例has_many包含多個has_many。我認爲它有一個非常直接的模式,但不能從apidockapi.rubyonrails.org中解決。我遇到的問題是將Supply_company添加到與product_image包含的has_many:through關係中。

三江源提前爲您的意見

更新我已經摸索出如何顯示belongs_to的......感覺有點啞上,因爲它是非常簡單,你只需要一些時間來思考

<% pd.product_images.each do |pd| %> 
    <p> <%= pd.product_colour.product_colour %></p> 
<% end %> 

/app/models/product.rb

class Product < ActiveRecord::Base 
    belongs_to :product_type 
    belongs_to :product_category 
    belongs_to :product_colour 
    belongs_to :product_size 

    has_many :product_supply_companies, :foreign_key => 'product_id' 
    accepts_nested_attributes_for :product_supply_companies, :allow_destroy => true 
    has_many :supply_companies, :through => :product_supply_companies 
    accepts_nested_attributes_for :supply_companies 

    has_many :product_images, dependent: :destroy, :foreign_key => 'product_id' 
    accepts_nested_attributes_for :product_images, :allow_destroy => true 
end 

應用程序/模型/ product_supply_company.rb

class ProductSupplyCompany < ActiveRecord::Base 
    belongs_to :product 
    belongs_to :supply_company 

# accepts_nested_attributes_for :supply_company 
# accepts_nested_attributes_for :product 

end 

應用程序/模型/ supply_company.rb

class SupplyCompany < ActiveRecord::Base 

    has_many :products, :through => :product_supply_companies 
    has_many :product_supply_companies, :foreign_key => 'supply_company_id' 

    accepts_nested_attributes_for :products 
    accepts_nested_attributes_for :product_supply_companies, :allow_destroy => true 

end 

應用程序/模型/ product_colour.rb

class ProductColour < ActiveRecord::Base 
    has_many :products 
end 

數據庫方案

create_table "product_categories", force: true do |t| 
    t.string "product_category" 
    t.string "product_category_description" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "product_colours", force: true do |t| 
    t.string "product_colour" 
    t.string "product_colour_description" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "product_images", force: true do |t| 
    t.integer "product_id",     null: false 
    t.datetime "created_at",     null: false 
    t.datetime "updated_at",     null: false 
    t.string "product_image_file_name" 
    t.string "product_image_content_type" 
    t.integer "product_image_file_size" 
    t.datetime "product_image_updated_at" 
    t.boolean "default_image" 
    end 

    create_table "product_sizes", force: true do |t| 
    t.string "product_size" 
    t.string "product_size_description" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "product_supply_companies", force: true do |t| 
    t.integer "product_id" 
    t.integer "supply_company_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "product_types", force: true do |t| 
    t.string "product_type" 
    t.string "product_type_description" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "products", force: true do |t| 
    t.string "product_name" 
    t.text  "product_description" 
    t.integer "product_type_id" 
    t.integer "product_category_id" 
    t.string "product_colour_id" 
    t.integer "product_size_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 
+0

加入協會在您的文章 –

+0

車型都增加了。不知道如何將supply_company has_many :: through關係添加到已經爲product_images完成的關係。 –

+0

您的產品表中是否有product_type_id,product_category_id,product_colour_id,product_size_id? –

回答

1
@products = Product.includes(:product_images, :colour, :supply_companies) 
        .where(product_images: {product_id: product_ids, :default_image => true}) 
        .select('products.*, product_colours.product_colour') 

這與所有關聯查詢。

index.html.erb

<% @products.each do |pd| %> 
    <%= content_tag :div, :class => "col-md-3 col-sm-6 hero-feature" do %> 
    <% pd.product_images.each do |i| %> 
     <div class="thumbnail"> 
      <%= image_tag (i.product_image(:medium)) %> 
     </div> 
    <% end %> 
    <div class="caption"> 
     <h3><%= pd.product_name %></h3> 
     <p><%= pd.product_description %></p> 
     <p><%= pd.product_colour %></p> 
    </div> 
    <% end %> 
<% end %> 

Product.rb

belongs_to :colour, class: 'ProductColor', foreign_key: 'product_colour_id' 
+0

這就是我的。通過一些試驗和錯誤,似乎我根本不需要使用includes來訪問has_many:through關係或belongs_to,它對於根據產品ID和default_image布爾過濾它們所需的圖像非常有用。我一直在想這個問題都是錯的。它可以通過上面的代碼直接在index.html.erb中完成。我會發布工作代碼。感謝您推送user123 :-) –

+0

我沒有找到你 –

+0

註冊求助 –

0

對於那些跟隨。 User123給了我一些想法,最終提供了一個解決方案。我一直以來都在思考這個問題。

應用程序/控制器/ home_controller.rb

class HomeController < ApplicationController 
    def index 
    products = Product.last(5) ## find last five products 
    product_ids = products.map(&:id) ## map their ids so I can retrieve 
    images with matching id's 

    ## create array of products with corresponding default_images. 
    ## Not necessary to include has_many, has_many :through or 
    ## belongs_to as they can be added directly on index.html.erb page 

    @products = Product.includes(:product_images) 
        .where(product_images: {product_id: product_ids, :default_image => true}).last(5) 

    end 
end 

/app/views/home/index.html.erb

<% @products.each do |pd| #1st level directly access product_attributes %> 
     <%= content_tag :div, :class => "col-md-3 col-sm-6 hero-feature" do %> 

      <% pd.product_images.each do |i| # 2nd access has_many with 
      foreign key in different table %> 
       <div class="thumbnail"> 
       <%= image_tag (i.product_image(:medium)) %> 
       </div> 
      <% end %> 

      <div class="caption"> 
      <h3><%= pd.product_name %></h3> 
      <p><%= pd.product_description %></p> 
      <p><%= pd.product_colour.product_colour #first level access 
        belong_to relationship attributes %></p> 

      <p><% pd.supply_companies.each do |sc| # 2nd level again to 
        access has_many :through relationship only done again for 
        layout purposes. %></p> 
        <%= sc.company_name %> 
      <% end %> 
      </div> 
     <% end %> 
    <% end %>