开发者

Controller setup with three models?

I have a three models: Homework, Question, and HomeworkAttempt.

homework.rb:

class Homework < ActiveRecord::Base
  belongs_to :group
  has_many :questions
  has_many :homework_attempts
end

questi开发者_运维知识库on.rb:

class Question < ActiveRecord::Base
  belongs_to :homework
end

homework_attempt.rb:

class HomeworkAttempt < ActiveRecord::Base
  belongs_to :homework
  belongs_to :student
  has_many :answer_attempts
end

I setup two controllers, thinking this would be the best way. My homework controller is empty, and exists simply for nesting the homework_attempt. My homework attempts controller looks like this:

class HomeworkAttemptsController < ApplicationController
  def new
    @group = Group.find(params[:group_id])
    @homework = Homework.find(params[:homework_id])
    @questions = @homework.questions
    @homework_attempt = HomeworkAttempt.new
    current_user.homework_attempts << @homework_attempt
    @title = @homework.name
  end
  ...
end

I want to set it up so that the user can create a new homework attempt based on the homework. How should I go about doing this? What columns will I need in my model?

Thanks!


I can't tell exactly what you're asking, but I have some ideas that might help you get started:

In your relations, it seems like you're trying to use models that you never created. For example, belongs_to :group and has_many :answer_attempts both imply that those models exist, but you haven't mentioned them anywhere else. If you want to use these relations, you must create these models.

Also, it seems like you're doing too much work in your new action. The fourth line is usually all that's needed in such an action, but it is hard to tell without more detail on what exactly you're trying to accomplish.

You will need much more than you've shown here to make this all work. You really ought to have a controller to manage the CRUD for each of your models, unless you have some other plan that I'm not seeing. If you provide more information on what you currently have and what you're trying to get to, I can edit my answer to try to be more helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜