HAYATO SHIBA
HAYATO SHIBA

Reputation: 151

ActiveRecord::Associations::CollectionProxy

I have a question. views/admins/show.html.erb

<% provide(:title , "施設") %>


  <p><%= @admin.name  %></p>
  <p><%= @admin.place %></p>
  <p><%= @admin.address %></p>
  <p><%= @admin.content  %></p>
<%= @dogs.each do |dog| %>
  <p><%= dog.name %></p>
  <p><%= dog.age  %></p>
  <p><%= dog.personality  %></p>
  <p><%= dog.breed  %></p>
  <p><%= dog.content  %></p>
<% end %>

and admins_controller

class AdminsController < ApplicationController
 before_action :authenticate_admin! , only:[:show]    
 def show
    #@admin = current_admin
    @admin=Admin.find(params[:id])
    @dogs = @admin.dogs
 end
end

When I watch the show.html.erb, The page's last sentence contains ActiveRecord::Associations::CollectionProxy. Why do it contain?

Upvotes: 1

Views: 411

Answers (2)

j-dexx
j-dexx

Reputation: 10416

It's because you have the equals here, which tells rails to output it

<%= @dogs.each do |dog| %>

Make it

<% @dogs.each do |dog| %>

Upvotes: 2

Alok Swain
Alok Swain

Reputation: 6519

They hold the relation between Admin and Dog. It also provides many methods which can be used on the related Dog objects. Refer this for more details: http://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html

Upvotes: 0

Related Questions