views:

18

answers:

1

I have developed an application and I seem to be having some problems with my associations. I have the following:

class User < ActiveRecord::Base
  acts_as_authentic

  has_many :questions, :dependent => :destroy
  has_many :sites , :dependent => :destroy
end

Questions

class Question < ActiveRecord::Base
  has_many :sites, :dependent => :destroy
  has_many :notes, :through => :sites
  belongs_to :user
end

Sites (think of this as answers to questions)

class Site < ActiveRecord::Base
  acts_as_voteable :vote_counter => true

  belongs_to :question
  belongs_to :user
  has_many :notes, :dependent => :destroy
  has_many :likes, :dependent => :destroy

  has_attached_file :photo, :styles => { :small => "250x250>" }

  validates_presence_of :name, :description

end

When a Site (answer) is created I am successfully passing the question_id to the Sites table but I can't figure out how to also pass the user_id. Here is my SitesController#create

 def create
       @question = Question.find(params[:question_id])
       @site = @question.sites.create!(params[:site])

       respond_to do |format|
           format.html { redirect_to(@question) }
           format.js
         end
       end
A: 

I'd think this would do the job

@question = current_user.questions.find params[:question_id]

if not, then just assign mannualy.

@site = @question.sites.build(params[:site])
@site.user = current_user
@site.save
Jakub Hampl
Just tried that and still successfully passing question_id but not user_id.
bgadoci
that did it. Thanks.
bgadoci