2015-07-01 59 views
-1
class Employee < ActiveRecord::Base 
    has_many :sim_employees, dependent: :destroy 
    has_many :sims, through: :sim_employees 
end 

在我的sim_employees表中我有employee_id sim_d和is_local :: boolean。查詢通過關聯從has_many獲取數據

id employee_id sim_d is_local 
1 1   2  1 
2 1   3  0 
3 1   5  0 
4 2   1  0 
5 2   8  0 

所以現在我的要求是

員工/ index.html.erb

<%@employees.each do |employee|%> 

<%=employee.name%> 
# here I need a query if employee.id is_local is 1 for all employee than it will show local. If it is a combination of both 1 and 0 than it will show local/std if its 0 then it will show std. Please let me know how I will fetch this data from sim_employees table here. 
<%end%> 

現在我需要一個查詢,如果employee.id is_local設置爲1的所有員工比它會顯示本地。如果它是1和0的組合,那麼它將顯示本地/標準,如果它的0,那麼它將顯示標準。請讓我知道我將如何從sim_employees表中獲取這些數據。提前致謝。

回答

1
def of_call_type 
    call_types = self.sim_employees.map(&:is_local) 
    if call_types.include?(true) and call_types.include?(false) 
     "Local/STD" 
    elsif call_types.include?(true) 
     "Local" 
    else 
     "STD" 
    end 
end 

寫這個方法employee.rb

鑑於

<%@employees.each do |employee|%> 

<%= employee.name%> 
<%= employee.of_call_type %> 
<%end%> 
+0

@dinshaw raje是它的工作? –

+0

是的,它的工作原理:) – railslearner

+0

謝謝你這麼多 – railslearner