views:

461

answers:

1

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.

+2  A: 

instead of calling the column created_by, the preferred way is to name the column user_id . using these names for foreign keys will let rails "see" the associations automatically.

A simple way to set an attribute in the controller is to use a block as such:

@question = Question.new(params[:question]) do |q|
q.user_id = current_user.id
end
@question.save
bdon
ahh, awesome, so I see... This put the id of the current user in that field. However I did go against your recommendation and used a created_by column.
Joseph Silvashy
So of course this will sound dumb... but in my view I'm not sure how to access the user? question.created_by.username doesn’t work. but question.created_by gives me the id of the user. thoughts?
Joseph Silvashy
if you're going to call the column created_by, you need to instead specify the name of the column, as it will by default assume it's called user_id ( belongs_to :user, :foreign_key => "created_by" )then you can access that associated user: (question.user.username)
bdon
Wow, it was much less complicated than I thought, my problem was mostly in the association, which i defined: has_one :user, :through => :created_by, :foreign_key => "created_by"
Joseph Silvashy
i'm guessing that you mean an answer belongs_to :user. saying answer has_one :user implies that your user table has a column referencing an answer, which would be weird. Also, by saying a user :has_many :answers :through :questions, it implies that an answer posted by user B in response to a question by user A, belongs to user A. I'm not sure if this is the behavior you want.
bdon
you are exactly right on that account, that clears a lot up for me with associations. cool man! thanks big time.
Joseph Silvashy