叹息......我觉得这个是个大新手,所以可以说我有几个模型:
class Question < ActiveRecord::Base
has_many :answers
belongs_to :user
end
class Answer < ActiveRecord::Base
belongs_to :question
has_one :user
end
class User < ActiveRecord::Base
has_many :questions
has_many :answers, :through => :questions
end
所以我的问题是我不知道如何获取创建问题或答案的用户,应该在创建问题(或答案)时确定用户,并且用户应该来自当前用户的会话(来自 authlogic 的用户模型和控制器)请参见此处:
class ApplicationController < ActionController::Base
helper_method :current_user_session, :current_user
...
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
end
现在, current_user 辅助方法工作正常,但我如何设置创建问题或答案的用户?像 id 一样只想说 @question.user
顺便说一句,我的问题架构有一个 created_by 列,但是当我创建一个新问题时,它保持为空。