kolobrod21
@kolobrod21

Как отобразить модели которые принадлежать другой модели?

Как отобразить модель logo в view/posts/show ?
У каждого post есть много logo

logo.controller
class Logo < ActiveRecord::Base
  belongs_to :post
end

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


Такой код в view/posts/show работает
<%= @post.logos.count %>
и такой в /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  [["id", 4]]
=> #<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  [["id", 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


Что писать в контроллере ?
  • Вопрос задан
  • 106 просмотров
Решения вопроса 1
Dem1
@Dem1 Куратор тега Ruby on Rails
Ruby on Rails developer
в контроллер
def show
  @post = Post.includes(:logos).find(params[:id])
end

во view, к примеру, так
<table>
  <thead>
    <tr>
      <th>Зоголовок</th>
    </tr>
  </thead>
  <tbody>
    <% @post.logos.each do |logo| %>
    <tr>
      <td><%= logo.title %></td>
    <tr>
   <% end %>
  <tbody>
</table>
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы