Miguel Horta Nery
Miguel Horta Nery

Reputation: 5

Rails 4 - Retrieve grandchildren on association

I'm having a problem with association in my forum app in rails 4. It's probably something simple, but I can't find any solution that solves my problem (also I'm new to rails). This is the forum hierarchy: Theme > Topic > Post (something like comments)

What I want is to, in the Topics show action, retrieve it's own posts.

In my models I have

#Theme model
class Theme < ActiveRecord::Base
    has_many :topics, dependent: :destroy
    has_many :posts
end

#Topic model
class Topic < ActiveRecord::Base
    belongs_to :theme
    has_many :posts, dependent: :destroy
end

#Post model
class Post < ActiveRecord::Base
    belongs_to :theme
    belongs_to :topic
end

In my Theme and Topic controllers, I have this (and it works!):

#show action on Themes Controller
class ThemesController < ApplicationController
    def show 
        @theme = Theme.find(params[:id])
        @topics = @theme.topics
    end
end

#show action on topics controller
class TopicsController < ApplicationController
def show
    @theme = Theme.find(params[:theme_id])
    @topic = @theme.topics.find(params[:id])
end

This works, in my views I can show what themes I got and what topics they got. To access my posts inside the topic, I was trying;

#still inside show action on topics controller
class TopicsController < ApplicationController
def show
    @theme = Theme.find(params[:theme_id])
    @topic = @theme.topics.find(params[:id])
    @posts = @theme.topics.posts
end

But it totally doesn't works! I got the error

NoMethodError in ForumTopicsController#show
undefined method `posts' for #<ForumTopic::ActiveRecord_Associations_CollectionProxy:0x007fddd8c14158>

And if I try to create new posts going directly to it's 'new action' url, it works and I assume it saves the post associated to it's topic and theme. Here's my routes:

Rails.application.routes.draw do
resources :forum_themes do
    resources :forum_topics do
        resources :forum_posts
    end
 end

All I want is to access my Posts in Topics view ;-; I tried some things related to "collect", but it didn't work! (Perhaps I was the one who didn't work at all :P)

Please, help!

UPDATE

These are my migration files, don't know if it can help...

#migration file of themes
class CreateThemes < ActiveRecord::Migration
    def change
        create_table :themes do |t|
            t.string :title
            t.text :description
            t.timestamps null: false
        end
    end
end
#migration file of topics
class CreateTopics < ActiveRecord::Migration
    def change
        create_table :topics do |t|
            t.string :author
            t.string :title
            t.text :body
            t.references :theme, index: true
            t.timestamps null: false
         end
   end
end
#migration file of posts
class CreatePosts < ActiveRecord::Migration
    def change
        create_table :posts do |t|
            t.string :title
            t.text :content
            t.references :topic, index: true
            t.references :theme, index: true
            t.timestamps null: false
         end
    end
end

Upvotes: 0

Views: 119

Answers (1)

Pavan
Pavan

Reputation: 33542

undefined method `posts' for ForumTopic::ActiveRecord_Associations_CollectionProxy:0x007fddd8c14158

The problem lies here @posts = @theme.topics.posts. @theme.topics returns all topics of the associated theme, so you just can't append posts to it.

Instead, you need to loop through @theme.topics in the view like below to show the posts associated to topics

<% @theme.topics.each do |topic| %>
  <%= topic.posts %>
<% end %>

Upvotes: 1

Related Questions