Rails Has and Belongs To Many Associations -


my question has associations. forgive me learning this.

i have 2 tables (parent & child). have set associations parent table set has_many children , child table has 1 parent foriegn id set parent_id. working fine.

my question want create family view shows both parents , children , associated each other. this:

parent name

  • child name
  • child name

i pretty sure need create family table , has_and_belongs_to_many association not sure how show 2 above hope makes sense.

if want show parents , children @ view don't need create new table, below:

in parents controller's index action( considering want show parents children using index action ) :

def index   @parents= parent.all.includes(:children) end 

then on index.html.erb view :

<% @parents.each |parent| %>   <%= parent.name%><br/>   <% parent.children.each |child| %>     <%= child.name%><br/>   <% end %> <% end %> 

considering have define association between parent child model


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -