【问题标题】:Rails 4 includes multiple has_many and belongs_to index.html.erbRails 4 包含多个 has_many 和 belongs_to index.html.erb
【发布时间】:2015-06-03 01:38:54
【问题描述】:

我将如何处理包含以下内容的belong_to。而不是显示 product_colour_id id,而是显示关联的颜色(更新:解决了下面的这部分)。 product_colour_id 位于 Product 表中,并与相应的 Product_colour id 匹配。

我无法解决两个或多个 has_many 关联的情况。能做到吗?

app/controller/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 包含的示例。我认为它有一个非常直接的模式,但无法从apidockapi.rubyonrails.org 中解决。我遇到的问题是添加 Supply_company 这是一个 has_many :through 关系到我已经与 product_image 包含的那个关系。

提前感谢您的建议

更新我已经弄清楚了如何显示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

app/models/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

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

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

【问题讨论】:

  • 在您的帖子中添加关联
  • 已添加模型。不确定如何添加 supply_company has_many, :through 关系到已经为 product_images 完成的关系。
  • 您的 products 表中有 product_type_id、product_category_id、product_colour_id、product_size_id 吗?
  • 正确,除了 supply_company 的多对多关系。我可以做一个has_many 和belong_to,但不确定如何实现has_many :through 或多个has_many 关系。那么如何扩展它以包含供应商 @product_images = Product.includes(:product_images) .where(product_images: {product_id: product_ids, :default_image => true})
  • 添加了答案。你的解决方案让我思考,结果我试图让关联过于复杂,实际上可以在不添加更多包含的情况下实现。为您的回答喝彩,它实际上将我引向了一个不同的解决方案。 :-)

标签: ruby-on-rails-4 activerecord twitter-bootstrap-3 nested-includes


【解决方案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 %>

产品.rb

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

【讨论】:

  • 这就是他们的。通过一些试验和错误,我似乎根本不需要使用包含来访问 has_many :through 关系或 belongs_to 它对图像很有用,因为我需要根据产品 ID 和 default_image 布尔值过滤它们。我一直在想这个问题都错了。大部分都可以通过上面的代码直接在 index.html.erb 中完成。我将发布工作代码。感谢 user123 的推送 :-)
【解决方案2】:

对于后面的人。 User123 给了我一些想法,最终提供了解决方案。我一直在想这个问题。

app/controller/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 %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    相关资源
    最近更新 更多