我使用的設計進行驗證,並感到我的用戶與他們可以在登錄子域相關聯的方式難倒可以登錄下多個子域。我研究了plataformatec提供的信息here以及Dave Kenedy在Ruby Source here上的傑出文章。儘管如此,我仍然不完全理解聯合和查詢的好處。合併在軌3.2多個模型,以便用戶使用設計
class User < ActiveRecord::Base
has_many :residences
...
end
class Residence < ActiveRecord::Base
belongs_to :user
belongs_to :apartment
...
end
class Apartment < ActiveRecord::Base
has_many :residences
belongs_to :building
...
end
class Building < ActiveRecord::Base
attr_accessible :subdomain
has_many :apartments
...
end
在config/initializers/devise.rb
我說:
config.request_keys = [:subdomain]
在app/models/user.rb
我把它換成默認色器件的方法,`self.find_for_authentication有:
def self.find_for_authentication(conditions={})
conditions[:residences] = { :subdomain => conditions.delete(:subdomain) }
find(:first, :conditions => conditions, :joins => :residences)
end
當我跑我的規格我得到以下錯誤:
PG::Error: ERROR: column residences.subdomain does not exist
我知道,不知怎的,我有我的residence
表連接一路到building
表來確認用戶登錄隸屬於用正確的子域的建築物,但我不知道該怎麼做。有人有主意嗎? Rails Docs有關於連接表的信息,但也讓我感到困惑。 (對數據庫的基本信息和連接表也將會有所幫助。;-))
更新
我在app /模型/ user.rb已經修訂
def self.find_for_authentication(conditions={})
subdomain = conditions.delete(:subdomain)
building = Building.find_by_subdomain(subdomain)
apartments = Apartment.where('apartment.building_id' => building.id)
conditions[:residences] = { :apartment_id => apartments }
find(:first, :conditions => conditions, :joins => :residences)
end
這可能是有點接近我所需要的,但我還是發現了以下錯誤在rspec的:
1) UserSubdomainLogins signin should not be able to signin to building without access
Failure/Error: click_button "Sign in"
ActiveRecord::StatementInvalid:
PG::Error: ERROR: missing FROM-clause entry for table "apartment"
LINE 1: ...SELECT "apartments"."id" FROM "apartments" WHERE "apartment...
^
: SELECT "users".* FROM "users" INNER JOIN "residences" ON "residences"."user_id" = "users"."id" WHERE "users"."email" = '[email protected]' AND "residences"."apartment_id" IN (SELECT "apartments"."id" FROM "apartments" WHERE "apartment"."building_id" = 2895) ORDER BY last_name ASC LIMIT 1
哪個模型有子域場? – 2013-05-08 14:29:54
對不起,@ShawnBalestracci,我應該更加明確。建築物具有子域屬性。 – BenU 2013-05-08 15:34:37