1

I have three models , Programs Subprograms1 Subprograms2

Programs -> has_many :subprograms1 , :subprograms2

Subprograms1 -> has_and_belongs_to_many :Programs

Subprograms2 -> has_and_belongs_to_many :Programs

I want to get all Subprograms1 and Subprograms2 belongs to a particular Programs.

Is it possible in rails.

Thanks in advance......

Cyber
  • 4,844
  • 8
  • 41
  • 61
  • exact duplicate of [combine results from two queries and order by created\_at? \[rails 3\]](http://stackoverflow.com/questions/6633413/combine-results-from-two-queries-and-order-by-created-at-rails-3) – Jeff Paquette Dec 01 '12 at 17:28

2 Answers2

0

If I understand correctly you should be able to access all the Subprograms1 and Subprograms2 by just accessing the properties in the Programs model

p = Programs.find(10)
sp1 = p.Subprograms1 
sp2 = p.Subprograms2
Carlos Blanco
  • 8,592
  • 17
  • 71
  • 101
0

If the structures in your data are flat (there are no subprograms below other subprograms) you can use Active Record's Polymorphic Associations to use references to multiple other tables (not only one), so you could set up references from Programs to Subprograms1 and to Subprograms2 and retrieve them them all with a single call.

If the structures in your data will be deep (subprograms can have other subprograms) you should rather put all of your 3 tables into one table and use a Nested Set Model to manage the data tree and retrieve all (deep) subprograms associated with a program. There are many Ruby Gems that can help you in achieving this.

hoeni
  • 3,031
  • 30
  • 45
  • here my subprograms1 ,subprograms2 has_and_belongs_to_many :programs ,does polymorphic Association can handle this. – Cyber Dec 01 '12 at 17:48