activerecord - Rails 4 includes multiple has_many and belongs_to index.html.erb -


how handle belong_to following include. instead of displaying product_colour_id id show associated colour (update:solved part below). product_colour_id in product table , matches corresponding product_colour id.

its case of 2 or more has_many associations cant work out. can done?

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 |pd| %>           <%= content_tag :div, :class => "col-md-3 col-sm-6 hero-feature" %>           <% pd.product_images.each |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. assume there straight forward pattern cant work out apidock or api.rubyonrails.org. problem i'm having adding supply_company has_many :through relationship 1 have product_image include.

thankyou in advance advise

update have worked out how display belongs_to... feeling little dumb on easy needed time think

 <% pd.product_images.each |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 

database schema

 create_table "product_categories", force: true |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 |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 |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 |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 |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 |t|     t.string   "product_type"     t.string   "product_type_description"     t.datetime "created_at"     t.datetime "updated_at"   end    create_table "products", force: true |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 query associations.

index.html.erb

<% @products.each |pd| %>     <%= content_tag :div, :class => "col-md-3 col-sm-6 hero-feature" %>     <% pd.product_images.each |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' 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -