3 Replies Latest reply on Jul 11, 2012 6:26 AM by Steve Zhang

    How to deal with 2 models with one to many relationship?

      Right now we need to deal with one to many relationship for 2 models. For example:

      company vs. employee


      1. show the details of all the employees

      2. show the details of one company's employees

      3. delete a company, delete its employees as well.




      - we need 2 models, and 2 controllers, but how can we navigate between these 2 controllers? and reuse the view for list the employees?

      - Could  you provide a sample code to deal this kind of situation?



        • Re: How to deal with 2 models with one to many relationship?

          To navigate between models, you need to specify which controller.rb file you wish to access.

          This can be done like this:

          <a href="<%= url_for( :controller => :controller1, :action => :action1) %>" >Perform controller1's action1 from within controller2 </a>

            • Re: How to deal with 2 models with one to many relationship?

              Thanks Arquimidez,


              Now one of the major issue for me is how to show the details of the employees, for example:

              1. Show list of all the employees, this is definitely should be in the employee controller;

              2. show the detail of an employee, this is in the Employee controller.


              But I have a question of rendering employees of a compnay:

              List of all the employees of a company/ show details of an employee,  should I do that in the employee controller or company controller?

                  If I put that in the employee controller, then I need to figure out how to call from one controller to another controller, and I need to figure out how to navigate from employee controller back to company controller.

                    ex. the flow like this:

                       Company list -> company details -> employee list -> employee details

                    moving back will be

                       employee details -> employee list -> company details -> company list

                   How do I implement it in the employee controller?


              Or I can put the logic in the Company controller, which mean the company controller will deal with 2 models: company and employee, which will cause another issue, I have to duplicate the code base from employee into company controller, which is also not good.


              I really appreciate if you could provide the similar sample project which address this issue, so that I can get an insight.




            • Re: How to deal with 2 models with one to many relationship?



              For do that, you need to get the id of your company on the company controller, and then associate that company when you create an employee.


              For example, if you create an employee from the  employee controller you put a dropdown list whit the id's of your company controller, you get that id's simply putting


              @companys = Company.find(:all);


              then on your view you have to put



                       <div data-role="fieldcontain" data-theme="a">

                              <label for="employee[company]" class="fieldLabel">Select a Company:</label><br/>

                                    <select name="employee[company]" id="employee[company]" data-native-menu="false" data-theme="a">

                                          <% @companys.each do |company|%>

                                                       <option value="<%= company.object%>" > <%= company.Name %> </option>

                                          <% end %>




              Or you can add employees to your company from the company controller, you put an add buton on your company show.erb

              and then you can redirect to a view with all your employees and when you click on one of them you redirect to a method on your

              company controller that has this code


              def associate_employee

                    employee = Employee.find(@params['employee_id'])

                    employee.update_attributes({"company" => company_id })

                    redirect :controller => :Company, :action => :show, :id => company_id



              and when you delete a company you put this code for delete all his employees


              def delete

                  @company =Company.find(@params['id'])

                  if @company

                       employees = Employee.find(:all, :conditions => {"company" => @company.object})

                       employees.each do |employee|





                  redirect :action => :index 




              And basically thats how you work with that, you just need to write your code for the views