我有一個功能,如:包括與條件,但始終顯示主表結果
# get all locations, if the user has discovered them or not
def self.getAll(user)
self.find(:all, :order => 'min_level asc', :include => 'discovered_locations',
:conditions => [ "discovered_locations.user_id = ? OR discovered_locations.id is null", user.id])
end
自我實際上是BossLocation模型。我想獲取bosslocation的結果集和發現的位置,如果該位置是我的用戶發現的。但是,如果它沒有被發現,我仍然需要bosslocation並且沒有對象作爲發現的位置。通過上面的代碼,如果用戶沒有發現任何東西,我根本就沒有得到bosslocations。
編輯:
我的關聯是這樣的:
class BossLocation < ActiveRecord::Base
has_many :discovered_locations
has_many :users, :through => :discovered_locations
class DiscoveredLocation < ActiveRecord::Base
belongs_to :user
belongs_to :boss_location
class User < ActiveRecord::Base
has_many :discovered_locations
has_many :boss_locations, :through => :discovered_locations
我很困惑,你會建議什麼命令? – Spyros 2010-12-09 09:49:42
兩個發現手段'@locations = BossLocation.all @discovered = DiscoveredLocation.find(..)':P – rubyu2 2010-12-10 05:46:33