2

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
omarArroum
  • 255
  • 1
  • 8
  • 19
  • What's not working? If the graphic you entered is up-to-date, then `present` and `time_of_arrival` return `nil`, and `<%= nil %>` doesn't print anything. – Austin Mullins Mar 09 '13 at 21:50
  • Yes I understand that,but there's no way for me to manually update `present` or `time_of_arrival` attribute. Even through SQLite it won't let me update it – omarArroum Mar 09 '13 at 21:52
  • 1
    habtm is rarely if ever a good idea - See my answer here http://stackoverflow.com/questions/14243566/rails-habtm-joins/14244978#14244978 – jamesc Mar 09 '13 at 22:50

2 Answers2

2

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.

jamesc
  • 12,423
  • 15
  • 74
  • 113
  • 1
    Hey @jamesw, 1) doesn't work, I just played around with the code to clarify that these commands don't work, as I'm getting this error message: `undefined method 'present' for # `. 2 and 3) Give me this erros: `undefined method 'time_of_arrival' for #`. – omarArroum Mar 09 '13 at 22:18
  • 1
    What I'm trying to do is access these attributes, through console or web, doesn't matter, as long as I can access them – omarArroum Mar 09 '13 at 22:19
  • 1
    @teenOmar OK, you need to post up the contyroller actions AND all the models involved in this process. I'm pretty sure I know what is happening (You probably haven't set up a model to deal with the registers_student table). I'll update my answer. – jamesc Mar 09 '13 at 22:24
  • That's probably it, I haven't got a model to access it I think. Which controller actions do you need? They are just the template actions, haven't made any changed to any of them (Both `Register` and `Student`). For the models I will edit my question and include it as well – omarArroum Mar 09 '13 at 22:26
  • When you say, `set up your has_many and belongs_to relationships so you can use them in your view`, how exactly should i set them up (considering you stated that using HABTM is a bad idea. Sorry to bother, but could you just let me know how the associations should be in the `Student`, `Register` and `Student_Register` model? Thanks so much by the way ;) – omarArroum Mar 09 '13 at 23:03
  • @teenOmar In your case I would suggest that you take the "if it works don't fix it" approach regarding your existing relations, just bear in mind that it might not give you the flexibility you want. – jamesc Mar 09 '13 at 23:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25898/discussion-between-jamesw-and-teenomar) – jamesc Mar 09 '13 at 23:19
2

You need to implement has_many :through association.

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

Ramiz Raja
  • 5,942
  • 3
  • 27
  • 39