ruby on rails - How to access data through many_to_many relation? -


how access data through many_to_many relation?

here database creations:

the "useres":

class createsubgroups < activerecord::migration   def change     create_table :subgroups |t|       t.string :name       t.belongs_to :group        t.timestamps     end   end end 

the relationship:

class creatertimespans < activerecord::migration   def change         create_table :rtimespans |t|             t.belongs_to :subgroup_id             t.belongs_to :timespan_id         end     end  end 

the used:

class createtimespans < activerecord::migration   def change     create_table    :timespans |t|       t.string      :name       t.text        :descrition       t.integer     :start_h       t.integer     :start_m       t.integer     :end_h       t.integer     :end_m     end   end end 

how example access names of timespans, belong 1 subgroup? , vis versa, how can show, subgroups use specific timespan?

first question

you can whatever find call long instance. instance calling .first on model.

subgroup.first.timespans.each |timespan|    puts timespan.name end 

vis versa

timespan.first.subgroups.each |subgroup|    puts subgroup.name end 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -