简体   繁体   English

Rails 4包含多个has_many和belongs_to index.html.erb

[英]Rails 4 includes multiple has_many and belongs_to index.html.erb

How would I handle a belong_to with the following include. 我将如何处理包含以下内容的belong_to。 Instead of displaying the product_colour_id id like to show the associated colour ( Update:Solved this part below ). 与其显示product_colour_id id,不如显示其关联的颜色( Update:在下面解决了这一部分 )。 The product_colour_id is in the Product table and matches the corresponding Product_colour id. product_colour_id在“产品”表中,并且与相应的Product_colour ID匹配。

Its the case of two or more has_many associations that i cant work out. 这是我无法解决的两个或多个has_many关联的情况。 Can it be done? 能做到吗

app/controller/home_controller.rb 应用程序/控制器/ 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 /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>

I'm having difficulty finding examples of multiple has_many includes. 我很难找到多个has_many包含的示例。 I assume there is a very straight forward pattern to it but cant work it out from apidock or api.rubyonrails.org . 我认为它有一个非常简单的模式,但是无法从apidockapi.rubyonrails.org中解决它。 The problem i'm having is adding the Supply_company which is a has_many :through relationship to the one I already have with the product_image include. 我遇到的问题是,在与product_image include已经存在的has_many:through关系中添加了Supply_company。

Thankyou in advance for your advise 预先感谢您的建议

Update I have worked out how to display the belongs_to... feeling a little dumb on that as it was very easy just needed some time to think 更新我已经解决了如何显示belongs_to ...对此感到有点愚蠢,因为这很容易,只需要一些时间就可以思考

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

/app/models/product.rb /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

app/models/product_supply_company.rb 应用程序/模型/ 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

app/models/supply_company.rb 应用程序/模型/ 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

app/models/product_colour.rb 应用程序/模型/ product_colour.rb

class ProductColour < ActiveRecord::Base
  has_many :products
end

Database Schema 数据库架构

 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
@products = Product.includes(:product_images, :colour, :supply_companies)
                   .where(product_images: {product_id: product_ids, :default_image => true})
                   .select('products.*, product_colours.product_colour')

this the query with all associations. 此查询具有所有关联。

index.html.erb 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 Product.rb

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

For those that follow. 对于那些跟随。 User123 gave me some ideas that finally provided a solution. User123给了我一些想法,终于提供了解决方案。 I had been over thinking the problem mostly. 我大部分时间都在考虑这个问题。

app/controller/home_controller.rb 应用程序/控制器/ 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 /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 %>

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

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