2013-12-13 43 views
0

對於一個招聘應用程序,我有一個用戶模型,它有很多common_app。這種類型的過濾工作在belongs_to關聯中嗎?

每個用戶實際上只有1個common_app,但是如果將來他們有更多,我將它設置爲has_many關聯。

在普通的應用程序,我想有以下數據 - >

user_id:integer 
current_city:string 
grad_year:integer 
read_type:string 
listen_speak:string 
time_in_china:integer 
desired_industry:(not sure, will be a multi-select picklist) 
location_pref:(not sure, will be a multi-select picklist) 

我很困惑與雖然這樣做,是的應用程序的功能的一部分是根據他們的答案過濾用戶。

有了這種關聯,我能根據他們的答案篩選所有用戶嗎?即grad_year是2005年的所有用戶?

如果是的話,我會如何編寫命令來做到這一點?

回答

0
class User < ActiveRecord::Base 
    has_many :apps 
end 

class App < ActiveRecord::Base 
    belongs_to :user 
end 

User.includes(:apps).where('apps.grad_year = 2005') 
+0

太棒了!謝謝ceny。作爲一般建議,你認爲這是做到這一點的最好方法嗎?或者你會選擇不同類型的關聯? –

+0

我更新我的帖子,從用戶開始可能看起來更自然。模型設計基於您的描述。我需要更多的信息,如需求或用例來決定是否是最好的方式。 – nickcen