omarArroum
omarArroum

Reputation: 255

Trying to access attribute in join table Rails

Ok I think I have two problems which need to be fixed

I've got the following tables: Student, Register and a join table which displays the records that 1 register has many students in it, called 'Registers_Students' which looks like this: enter image description here.

I had to create the Register_Students table via rails g migration CreateStudentRegister, which looked like this:

class CreateStudentRegister < ActiveRecord::Migration
    def change
    create_table :registers_students, :id => false do |t|
    t.integer  :register_id
    t.integer :student_id
    t.boolean :present
    t.time :time_of_arrival
  end
 end
end

Now, I want every register to have a number of students, and for every student, I want them to have a present status of true/false, and every student should also have a time_of_arrival time. However, I have no way of accessing the time_of_arrival or present attributes, as I want to set them.

The way I want to change these attributes for every student, is in the register/show.html.erb using the following code:

<% @register.students.each do |student| %>
  <tr>
      <td><%= Register.find(@register).present %></td>         #Doesn't work
      <td><%= student.university_id%></td>
      <td><%= student.first_name.titlecase%></td>
      <td><%= student.last_name.titlecase%></td>
      <td><%= Register.find(@register).time_of_arrival %></td> #This doesn't work
      <td><%= student.time_of_arrival%></td>                   #Nor does this

  </tr>
  <% end %>
</table>

The reason I want it to be displayed in the show page, is so that the register can be edited with marking the student as either being present or absent, and also mark their time of arrival using a checkbox (That part hasn't been implemented yet, but if any of you guys know how to implement it, I'd love to hear about it).

Thanks guys for any answers in advance

EDIT: Added models

Model for Students:

class Student < ActiveRecord::Base
  has_and_belongs_to_many :registers
  validates :university_id, :length => { :minimum => 10}, #Checks that the University ID is 10 characters long
            :uniqueness => {:case_sensitive => false}, #Checks that the University ID is unique, regardless of case-sensitivity
        :format => { :with => /[wW]\d\d\d\d\d\d\d\d\d/, #University ID needs to be in the right format via regex
                     :message => "Needs to be in the right format i.e. w123456789"}

  default_scope :order => 'LOWER(last_name)' #Orders the students via last name
  attr_accessible :first_name, :last_name, :university_id

  def name
    first_name.titlecase + " " + last_name.titlecase
  end
end

And this is the other model for the Register

class Register < ActiveRecord::Base
  has_and_belongs_to_many :students
  belongs_to :event
  attr_accessible :date, :student_ids, :module_class_id, :event_id
end

Upvotes: 2

Views: 2896

Answers (2)

jamesc
jamesc

Reputation: 12837

Update You need to set up a model to access the registers_students table

That table name doesn't follow rails conventions or normal symantics so can I suggest you

1) roll back your db by 1 migration (assuming that the creation of this table was the last migration) using

rake db:rollback

2) delete the migration from the db/migrations folder

3) use the built in rails model generator

rails g model student_register register_id:integer student_id:integer #etc

Now migrate your database Then set up your has_many and belongs_to relationships so you can use them in your view.

You will now have for each student a student_registers array you can loop through and access the present field on End update

1)

  <td><%= Register.find(@register).present %></td>         #Doesn't work

You have an instance of @register already so there is no need to go abnd read the database again to find the same one. Just use the attribute you want directly on the @register object

<td><%= @register.present %></td>

you original code although it is bad design should have worked

If that doesn't work then you have a more fundamental problem that is not possible to guess at with the little information you have provided so come back and edit your question and leave a comment if this is the case.

2)

the same goes for

  <td><%= Register.find(@register).time_of_arrival %></td> #This doesn't work

3)

And because you say this doesn't work

  <td><%= student.time_of_arrival%></td>                   #Nor does this

I start to suspect your statements of not working and suggest that it is working perfectly Perhaps you have no data in those fields to display. So enter some data and all will be good.

4) There are 3 main ways to edit multiple records on a screen using rails helpers. I suggest you read up on fields_for

As a starting point http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

Also check out this http://railscasts.com/episodes/198-edit-multiple-individually Ryan Bates has done other railscasts on this subject so have a trawl through that website.

Upvotes: 2

Ramiz Raja
Ramiz Raja

Reputation: 6030

You need to implement has_many :through association.

See rails guide for this, http://guides.rubyonrails.org/association_basics.html

Upvotes: 2

Related Questions