K
K
kolobrod212016-05-05 18:23:02
Ruby on Rails
kolobrod21, 2016-05-05 18:23:02

How to display models that belong to another model?

How to display model logo in view/posts/show ?
Each post has many logo
logo.controller

class Logo < ActiveRecord::Base
  belongs_to :post
end

post controller
class Post < ActiveRecord::Base
  has_many :logos 
end

This code in view/posts/show works
<%= @post.logos.count %>
and this one in /view/logos/show
<%= @logo.post.title %>
irb(main):001:0> allPost = Post.all
  Post Load (4.5ms)  SELECT "posts".* FROM "posts"
=> #<ActiveRecord::Relation [#<Post id: 3, title: "Need assistance with modifying explainer video fil...", content: "We are looking to hire someone to make modificatio...", created_at: "2016-05-01 19:41:48", updated_at: "2016-05-01 19:41:48", customer_id: 7, old_logo_file_name: nil, old_logo_content_type: nil, old_logo_file_size: nil, old_logo_updated_at: nil>, #<Post id: 4, title: "I need a logo design", content: "I am beginning a new Amazon FBA startup and I am i...", created_at: "2016-05-01 22:18:58", updated_at: "2016-05-05 11:04:51", customer_id: 8, old_logo_file_name: "my-owl.png", old_logo_content_type: "image/png", old_logo_file_size: 28074, old_logo_updated_at: "2016-05-05 11:04:50">]>
irb(main):002:0> allPost.find(4)
  Post Load (0.4ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  
=> #<Post id: 4, title: "I need a logo design", content: "I am beginning a new Amazon FBA startup and I am i...", created_at: "2016-05-01 22:18:58", updated_at: "2016-05-05 11:04:51", customer_id: 8, old_logo_file_name: "my-owl.png", old_logo_content_type: "image/png", old_logo_file_size: 28074, old_logo_updated_at: "2016-05-05 11:04:50">
irb(main):003:0> allLogo = Logo.all
  Logo Load (0.3ms)  SELECT "logos".* FROM "logos"
=> #<ActiveRecord::Relation [#<Logo id: 1, post_id: 4, designer_id: 1, title: "First logo", created_at: "2016-05-05 14:15:27", updated_at: "2016-05-05 14:15:27">]>
irb(main):004:0> allLogo.find(1)
  Logo Load (0.3ms)  SELECT  "logos".* FROM "logos" WHERE "logos"."id" = ? LIMIT 1  
=> #<Logo id: 1, post_id: 4, designer_id: 1, title: "First logo", created_at: "2016-05-05 14:15:27", updated_at: "2016-05-05 14:15:27">
irb(main):005:0>

create_table "logos", force: :cascade do |t|
    t.integer  "post_id"
    t.integer  "designer_id"
    t.string   "title"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "posts", force: :cascade do |t|
    t.string   "title"
    t.text     "content"
    t.datetime "created_at",            null: false
    t.datetime "updated_at",            null: false
    t.integer  "customer_id"
    t.string   "old_logo_file_name"
    t.string   "old_logo_content_type"
    t.integer  "old_logo_file_size"
    t.datetime "old_logo_updated_at"
  end

What to write in the controller?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Demidenko, 2016-05-05
@kolobrod21

to the controller

def show
  @post = Post.includes(:logos).find(params[:id])
end

in view, for example, like this
<table>
  <thead>
    <tr>
      <th>Зоголовок</th>
    </tr>
  </thead>
  <tbody>
    <% @post.logos.each do |logo| %>
    <tr>
      <td><%= logo.title %></td>
    <tr>
   <% end %>
  <tbody>
</table>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question