如何才能使組織僅在用戶未登錄時才創建?在用戶模型中創建組織,除非current_user已登錄..?
這有點含糊,所以讓我稍微清楚一點。我有兩張桌子;一個組織表和另一個用戶表。組織有許多用戶,用戶屬於一個組織。
#型號/ organizations.rb
class Organization < ActiveRecord::Base
has_many :users
我做到了,這樣,當用戶註冊後,組織與他的帳戶一起創建。
#型號/ user.rb
class User < ActiveRecord::Base
belongs_to :organization
before_create :create_organization
private
def create_organization
self.organization = Organization.create :name => self.name
end
酷,工程。
但是用戶也應該能夠簽署其他人一個帳戶,而新用戶的organization_id
應該是一樣current_user.organization_id
* #控制器/ users_controller.rb *
def create
@user = User.new(params[:user])
if current_user
@user.organization_id = current_user.organization_id
end
end
該解決方案限制了我,因爲即使用戶已經登錄,模型也在創建組織。它們獨立工作(如果我取出模型中的before_filter
或控制器中的if current_user
),但不綁定療法。
由於您無法訪問用戶模型中的current_user,因此我一直很難讓它們一起工作。
我會將organization_id作爲參數鏈接放在註冊鏈接中,但這不是很安全,因爲這意味着任何用戶都可以簡單地更改URL中的organization_id,並自動將帳戶提供給不屬於另一個組織的組織給他們。
如果很重要,我使用Authlogic作爲我的身份驗證解決方案。
有什麼我可以在用戶模型或用戶控制器做到這一點?
耶穌,你會認爲這是如此艱難....我很驚訝於我經常忽略的這些簡單的解決方案。 D'哦!許多很多很多謝謝@mischa – cdotfeli 2011-12-21 09:34:13
歡迎您! – Mischa 2011-12-21 09:37:39