0

Im new with ruby on rails and I'm again blocked on something really similar as the last time.. I have a User table, a Course table and a Mark table. I can create a Mark with a user_id, a course_id and a grade.

In my models i have :

/* Mark model */
class Mark < ActiveRecord::Base
  belongs_to :user
  belongs_to :course
end

/* Course model */
class Course < ActiveRecord::Base
  has_many :marks
end

/* User model */
class User < ActiveRecord::Base
  has_many :marks
end

So what I want to do is when I created a new mark, with my form I want that my form disappear and display a button destroy ! Like this :

<% if @mark.course_ids.include?(@course.id) and @mark.user_ids.include?(user.id)
   # Button destroy
<% else %>
   <%= form_for Mark.new do |f| %>
       <%= hidden_field_tag :course_id, @course.id %>
       <%= hidden_field_tag :user_id, user.id %>
       Grade: <%= number_field_tag :grade, nil, min: 0, max: 100 %>
       <%= f.submit %>
    <% end %>
<% end %>

The goal is to avoid the form when the mark just be added previously.. But here obviously @mark.course_ids and @mark.user_ids doesn't exist !!

Thanks for you help !

7
  • "similar as the last time" – are you referring to another question? If so, provide a link, please. Commented Apr 12, 2017 at 12:44
  • Yes the other one was this : here Commented Apr 12, 2017 at 12:55
  • @mark contains course_id and user_id. So you may write if @mark.course_id == @course.id && @mark.user_id == user.id Commented Apr 12, 2017 at 12:56
  • @Velantin Mark belongs to user and Mark belongs to course . it means Mark will always have only one course and user. right ? Commented Apr 12, 2017 at 12:58
  • My view will be a show on my course so example : course/1. I find the @mark by making a findall where course_id: params[:id] ! This is to get every @mark in my table link to this course. So @mark is a table and can't call method user_id or course_id without a .each ! Commented Apr 12, 2017 at 13:03

2 Answers 2

1

First, I suggest you rethink your tables, maybe take a look at the many-to-many relationship.

Regarding your question, you want to know if there exists a mark that belongs to a specific course for a specific user, so you would need something like this :

Mark.where(course_id: @course.id, user_id: current_user.id).any?

This will return true if such a record exists.

Sign up to request clarification or add additional context in comments.

1 Comment

This could work but my User_id is not the current_user ! This is a id of every user in this course this need a .each method ...
0

A belongs_to association sets up a one-to-one connection with the other models. Rails then creates a singular utility method (among others) in your model.

For example, According to your model associations, a Mark belongs_to a Course and a User. Rails will create the following utility methods in your Mark model:

@mark.course
@mark.user

So the statement:

@mark.course_ids.include?(@course.id) and @mark.user_ids.include?(user.id)

is incorrect. Those methods do not exist in the Mark model.

Try this:

Controller

@marks = Mark.where(course_id: params[:id])

This will get all the marks for that course so the mark will definitely have a course associated with it (there's no need to make that check in your view).

View

Render a form for each mark:

<% @marks.each do |mark| %>
  <% unless mark.user.exists? %>
    <%= form_for Mark.new do |f| %>
      <%= hidden_field_tag :course_id, @course.id %>
      <%= hidden_field_tag :user_id, user.id %>
      Grade: #{number_field_tag :grade, nil, min: 0, max: 100}#{f.submit}
    <% end %>
  <% end %>
<% end %>

3 Comments

Thanks for you answer ! But If i want to use @mark.course for exemple, what shoul i have in my controller? Actually I have this @mark = Mark.where(course_id: params[:id]) but this is an array :/
@ValentinFerey .where will always return a collection of records. Also, a single course can have multiple marks so it makes sense that it returns an array of marks for the course. You have to loop through each one and display a form for it. Check my updated answer.
WoooW ! Thanks a lot !! Of course I new that was the only solution but I just didn't know about the "Unless" tag ! With this I can fix all my problems ! Thanks a lot ! I never heard about unless before ROR ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.