Sigh... I feel like a big newbie on this one, so lets say I have a few models:
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
so my issue is that I don't know how to get the user that created the question or answer, the user should be determined when the question (or answer is created) is created, and the user should come from the current user's sessions (from authlogic's user model and controller) see here:
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
Now, the current_user helper method works fine, but how can I set what user created the question or answer? like id like to just say @question.user
btw, my schema for my question has a created_by column, but when I create a new question it stays null.