0

How do I store arrays values in the database using ruby on rails? I am building a school timetable which have 6 periods a day. I am storing the subject ID into an array, but I just don't know how to save the id values of that array.

My Controller:

def create
  @timetable = Timetable.new(params[:timetable])

    @buildtimetable = params[:gradeclassroomsubject_id]
      @buildtimetable.each do |subjectID|
        subjectID.save!(params[:gradeclassroomsubject_id].reject { |k,v| v.blank? })
      end
end

class CreateTimetables < ActiveRecord::Migration
  def change
    create_table :timetables do |t|
      t.integer :period_no
      t.integer :day_no
      t.integer :gradeclassroomsubject_id
      t.integer :user_id

      t.timestamps
    end
  end
end

Any help is much appreciated I am way under pressure and this kinda feels embarrassing.

Many thanks

6
  • I can't understand your question very well. you want to create a model timetable, and there is another model called subject. You want to create a relationship between this two tables. Is my understanding right? Commented Jan 11, 2013 at 6:58
  • Hi Brian, my problem is on the viewer to controller. I have a list of subjectI_D stored in an array say six values in an array. when I click save how do I save them meaning in my model I must have 6 rows? I hope u can understand me. Many thanks Commented Jan 11, 2013 at 7:13
  • your model timetable has 7 days, each of which has 6 periods, and you want to link subject_id to each period? or each timetable is a day? Commented Jan 11, 2013 at 7:19
  • It's 5 days from Monday to Friday and has 6 periods. Commented Jan 11, 2013 at 7:36
  • hey Brian this is a sample link of what I need to save: schoolguru.co.za/teachertimetable Commented Jan 11, 2013 at 7:38

1 Answer 1

0

short answer: it's impossible to store many values(all values in an array) into a column of a table.

As you just want to store subject_id, I suppose you already have a table that stores all subjects and the only thing you want to do is setting a relationship between a specific user and the subjects he/she takes. A many-to-many relationship is the one you need.

create a new table:

class CreateStuendship < ActiveRecord::Migration
  def change
    create_table :stuedentships do |t|
      t.integer :subject_id
      t.integer :user_id
      # something else you want to store about this relationship, ex: year, semester...
    end
  end

app/models/studentship:

class Studentship < ActiveRecord::Base
  belong_to :user
  belong_to :subject
end

app/models/user.rb:

class User < ActiveRecord::Base
  has_many :studentship
  has_many :subjects, :through => :studentship
end

app/models/subject.rb:

class Subject < ActiveRecord::Base
  has_many :studentship
  has_many :users, :through => :studentship
end

furthermore, I think you can store your "period_no" and "day_no" into your subjects table, so you can know the time of the course by the subjects table. If the time of the course is not fixed( subjects with the same id have different course time), storing these two attributes into studentship may be a good idea. By doing so, you don't need the table timetables, which is unnecessary, anymore.

when you want to store the subjects taken by a user, just create new studentships according to your array iteratively.

Finally, you can

user = User.frist
subject = Subject.first
user.subjects  # subjects taken by the first user
subject.users  # users who take the first subject
Sign up to request clarification or add additional context in comments.

3 Comments

<% @subject_id.each do |ids| %> <%= text_field_tag 'gradeclassroomsubject_id[]', ids %> <% end %>
The relationship is between a classroom and subjects. example: Grade 9A can have multiple subjects. all this works fine the challenge here is to put this on the table that has the period no, subject and grade. so far with the code above I can store it on the array but when I click save I get this error message now "undefined method `stringify_keys' for"
Actually, you should mention this situation in your question.... here is the reason why you get this error: stackoverflow.com/a/1815730/938380 however, as I mentioned in the answer, it's impossible to store "many values" into "one column". I think replacing the User model I mentioned in the answer with your Classroom model is a much better idea. Also, it will be much easier to maintain in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.