Rails nested associations (model through a model) -


i have 3 models: projects contains stages contains contents.

on projects show.html.erb, want show stages associated project, contents associated stages.

so here's tried in show.html.erb:

<% @stages.each |stage| %>   <section name="concept" class="stage">     <h3><%= stage.name %></h3>      <% @contents.each |content| %>       <p><%= content.name %></p>     <% end %>    </section>   <% end %> 

and here's what's in projects_controller.rb

def show     project = project.friendly.find(params[:id])     @stages = project.stages      stage = stage.friendly.find(params[:id])     @contents = stage.contents   end 

here's stage.rb model:

class stage < activerecord::base     extend friendlyid     friendly_id :name, use: :slugged      belongs_to :project     has_many :contents end 

and here's contents.rb:

class content < activerecord::base     belongs_to :stage end 

the error i'm getting is:

sqlite3::sqlexception: no such column: stages.slug: select "stages".* "stages" "stages"."slug" = 'pulse' order "stages"."id" asc limit 1

extracted source (around line #16):

stage = stage.friendly.find(params[:id]) 

edit: issue it's looking stage id of project, not id of stage. i'm trying stage it's showing , iterate on each of contents belong it.

projects controller:

def show   @project = project.friendly.find(params[:id])   @stages = @project.stages.includes(:contents) end 

view:

<% @stages.each |stage| %>   <section name="concept" class="stage">     <h3><%= stage.name %></h3>      <% stage.contents.each |content| %>       <p><%= content.name %></p>     <% end %>    </section>   <% end %> 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -