2017-03-23 18 views
-3

我有三个模型作为followings.check出wheather我有关联他们correctly.sorry.am新轨道显示数据,其中指定==“经理”

class Designation < ActiveRecord::Base 
    has_many :employes 
    end 


    class Employe < ActiveRecord::Base 
    belongs_to :designation 
    has_many :branch 
    end 



    class Branch < ActiveRecord::Base 
    belongs_to :employe 
    end 

在我的雇工形式,我有添加一个选择框来选择指定。并显示在雇佣指数页面...现在的问题是我想显示雇员的名称,他们的指定作为经理在我的分支模型..如何做到这一点..... 这是我的分支控制器

def index 
     @branches = Branch.all 
    end 

    def show 
    end 

    def new 
    @branch = Branch.new 
    end 

,这为m Y分支器查看

 <table id="example1" class="table table-bordered table-striped"> 
       <thead> 
         <tr> 
         <th>Branch name</th> 
         <th>Branch address</th> 
         <th>Phoneno</th> 
         <th>Mangers name</th> 
         <th>Status</th>         
         <th>Action</th> 
         </tr> 
        </thead> 
          <tbody> 
           <% @branches.each do |branch| %> 
           <tr> 

            <td><%= branch.branch_name %></td> 
            <td><%= branch.branch_address %></td> 
            <td><%= branch.phoneno %></td> 
            <td><%= branch.employe_id %></td> 

             </div> 
             <td></td> 
             <td> 
        <% if branch.is_active == true %> 
       <%= link_to " Active ",edit_branch_path(branch),class: 
        "btn btn-warning"%> 
       <%else%><%= link_to 
         "Inactive",edit_branch_path(branch),class: "btn 
         btn-warning"%> 
        <%end%> 

        </td> 
        <td> 
      <div class="btn-group"> 
      <span class=" btn btn-info dropdown-toggle" 
      data-toggle="dropdown" href="#menu1">Action 
      <b class="caret"></b> 
       </span> 

      <ul class="dropdown-menu"> 
      <li><%= link_to 'Edit', edit_branch_path(branch) %></li> 
     <li><%= link_to 'Destroy', branch, method: :delete, data: { 
      confirm: 'Are you sure?' } %></li> 
      </ul> 
      </li> 
      </td> 
     </div> 
     </tr> 
     <% end %> 
     </tbody> 
     </table> 

回答

0

正确下述关联:

class Branch < ActiveRecord::Base 
    has_many :employes 
end 

class Employe < ActiveRecord::Base 
    has_one :designation 
    belongs_to :branch 
end 

class Designation < ActiveRecord::Base 
    belongs_to :employee 
end 

意见

<table id="example1" class="table table-bordered table-striped"> 
    <thead> 
    <tr> 
     <th>Branch name</th> 
     <th>Branch address</th> 
     <th>Phoneno</th> 
     <th>Mangers name</th> 
     <th>Status</th> 
     <th>Action</th> 
    </tr> 
    </thead> 
    <tbody> 
    <% @branches.each do |branch| %> 
     <tr> 
     <td><%= branch.branch_name %></td> 
     <td><%= branch.branch_address %></td> 
     <td><%= branch.phoneno %></td> 
     <% managers = [] %> 
     <% branch.employes.each{|e| managers << e.name if e.designation.name == "manager" } %> 
     <td><%= managers.uniq %></td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 
+0

Mysql2 ::错误:未知列'whereations'中的'designations.employe_id':SELECT'designations'。* FROM'designations' WHERE'designations'.'employe_id' = 1 LIMIT 1 am getting following error bro。 – aaquib

+0

可能在创建workers scaffold的同时我添加了指定:references ...是这个原因,现在该怎么办?plz帮助我 – aaquib

+0

您是否已将employe_id添加到指定中? – puneet18

0

我认为员工应该有一个名称,请检查下面的改装车型代码,

class Designation < ActiveRecord::Base 
    belongs_to :employee 
    end 


    class Employe < ActiveRecord::Base 
    has_one :designation 
    has_many :branches 
    end 



    class Branch < ActiveRecord::Base 
    belongs_to :employe 
    end 
相关问题