Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>short answer: it's impossible to store many values(all values in an array) into a column of a table.</p> <p>As you just want to store <code>subject_id</code>, 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 <code>many-to-many</code> relationship is the one you need.</p> <p>create a new table:</p> <pre><code>class CreateStuendship &lt; 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 </code></pre> <p>app/models/studentship:</p> <pre><code>class Studentship &lt; ActiveRecord::Base belong_to :user belong_to :subject end </code></pre> <p>app/models/user.rb:</p> <pre><code>class User &lt; ActiveRecord::Base has_many :studentship has_many :subjects, :through =&gt; :studentship end </code></pre> <p>app/models/subject.rb:</p> <pre><code>class Subject &lt; ActiveRecord::Base has_many :studentship has_many :users, :through =&gt; :studentship end </code></pre> <p>furthermore, I think you can store your "period_no" and "day_no" into your <code>subjects</code> 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 <code>studentship</code> may be a good idea. By doing so, you don't need the table <code>timetables</code>, which is unnecessary, anymore.</p> <p>when you want to store the subjects taken by a user, just create new studentships according to your array iteratively.</p> <p>Finally, you can </p> <pre><code>user = User.frist subject = Subject.first user.subjects # subjects taken by the first user subject.users # users who take the first subject </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload