0
我有以下型號幫助有太多複雜的ActiveRecord查詢連接
class User < ActiveRecord::Base
has_many :occupations, :dependent => :destroy
has_many :submitted_jobs, :class_name => 'Job', :foreign_key => 'customer_id'
has_many :assigned_jobs, :class_name => 'Job', :foreign_key => 'employee_id'
end
class Job < ActiveRecord::Base
belongs_to :customer, :class_name => 'User', :foreign_key => 'customer_id'
belongs_to :employee, :class_name => 'User', :foreign_key => 'employee_id'
belongs_to :field
end
class Occupation < ActiveRecord::Base
belongs_to :user
belongs_to :field
belongs_to :expertise
end
與Field
沿(只是名稱和ID)和Expertise
(姓名和軍銜的整數)。
我需要創建一個過濾器,就像下面的僞代碼
select * from jobs where employee_id == current_user_id
or employee_id == 0
and current_user has occupation where occupation.field == job.field
and if job.customer has occupation where occupation.field == job.field
current_user.occupations must include an occupation where field == job.field
and expertise.rank > job.customer.occupation.expertise.rank
你可以看看我很快會耗盡我的SQL知識與查詢這個複雜的。
你會怎麼做?正確的SQL會很好,但是如果一個Rails人員可以用ActiveRecord方法指向正確的方法,那也很棒。或者,也許我沒有很好地構建我的模型;我願意接受各種建議。
謝謝!
'end.empty?'讓我做了雙重考慮,但它是有效的。這是我期待在Ruby中做的一個很好的例子。我仍然可以從純粹的好奇心中探索適當的SQL,但這應該很好。謝謝! – Luke 2011-05-29 00:01:08
是的,一切都是紅寶石表達甚至方法的定義,請upvote如果你高興地標記它回答 – Fonsan 2011-05-29 09:31:57