简体   繁体   中英

Rails 3 accessing has_many through join model in another controller

I'm doing an online judge application, so I have a User model, a Problem model and a Solution model to make the many to many relation. In that Solution model I have an extra column called "state" where I plan to store the state of a problem for a certain user: solved, wrong anwser, not solved.

I'm trying to modify the index action in my problems controller to render the state of the problem in the problem list (so a user can see if he has solved a problem or not, like I said before). Nevertheless I'm having an "uninitialized constant Admin::ProblemsController::Solution" error when I access the view.

I'm really new to RoR and my experience so far has been really harsh, so I'll appreciate any leads. Here is the code in the controller and the view:

problems_controller.rb

def index
  @problems = Problem.all
    if current_user
      @solutions = Solution.includes(:problem).where(:user_id => current_user.id)
  end

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @problems }
  end
end

views/problems/index.html.erb

<% @problems.each do |problem| %>
  <tr>
    <td><%= problem.name %></td>
    <td><%= problem.code %></td>
    <td><%= problem.description %></td>
    <% if current_user %>
      <%= for solution in @solutions do %>
        <% if solution %>
          <td><%= solution.state%></td>
        <% else %>
          <td>Not Solved</td>
        <% end %>
      <% end %>
    <% end %>
    <td><%= link_to 'Show', problem %></td>
    <% if current_user && current_user.is_admin? %>
    <td><%= link_to 'Edit', edit_problem_path(problem) %></td>
    <td><%= link_to 'Delete', problem, method: :delete, data: { confirm: 'Are you sure?' } %></td>
    <% end %>
  </tr>
<% end %>

I'm not sure if that's the best way I should be accessing the Solutions table or if I should be doing that in another controller (in the users controllers? in a solutions controller file perhaps?).

I want to be clear of how to use that "Solutions" join table. I had a has_and_belongs_to_many before and changed it because of the extra column. I've read a lot about many to many relationships, but I can't understand it for this case =(

Just need to use:

problem.solution.state

Unless a problem may have many solutions, then it would need to be something like:

problem.solutions.first.state

However this will just give the state of the first, so I'd define a method in Problem which calculates a status (eg. If any of the solutions solve it then problem is solved)

For 1 problem, many solutions for a given user. In Solution.rb

scope :for_user, lambda {|user_id| :conditions => {:user_id => user_id}}

Then we can call:

problem.solutions.for_user(current_user.id).first.state

It might look a bit long but it's highly flexible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM