Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8967947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:21:35+00:00 2026-06-15T17:21:35+00:00

I have a class persons and a class subjects. at the moment they are

  • 0

I have a class persons and a class subjects. at the moment they are not connected at all. As a user one can only chose subjects from a list and inspect them.
Now I’d like to allow users to assign to subjects, thus the user class needs a list where it can store its subjects.
… and a table which maps subjects to persons.. (m to n)

I have not much experience with Ruby on Rails and I am afraid to destroy my application when trying to do this.
So far I found out that there are commands to do that (sth. like $ rails generate migration AddSubjectsToPersons), but I can’t find out what they do exactly, e.g.if it touches my model or controller classes..
In case that yes – is there something removed I wrote before?, if not – is it enough to add a field (a list) called lessons to my model and the connection to the db will be detected automatically?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:21:36+00:00Added an answer on June 15, 2026 at 5:21 pm

    I generated a migration file using the command rails generate migration CreatePersonsSubjects. The names are put together in alphabetical order (convention).
    The result of this command is an skeleton in which code can be entered to change database tables.
    This went to the “up” part:

    create_table :persons_subjects, :id => false do |t|
          t.integer :person_id, :null => false
          t.integer :subject_id, :null => false
        end 
    

    In the “down” part these actions can be reverted:

       drop_table :persons_subjects
    

    Finally I added a field in the person class (and only to the person class, by the way. But one could/should also add a similar line to the subject). The name is convention too.

      has_and_belongs_to_many :subjects
    

    The subjects can be used for example like this in the view:

    <div>Courses taken:
      <ul>
        <% @person.subjects.each do |subject| %>
          <li><%= link_to subject.name, subject, :class => 'action' %></li>
        <% end %>
      </ul>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to C#. I have a Persons class and a User class
I have a list of Persons inside a Company Class. public class Company{ IList<Person>
I have an simple class that I get from a webservice. public class person
I have 2 classes and in the Persons class I want to add the
I have set up a one-to-many relationship in my scaffolded Grails application: class Course
What I have now: class User < ActiveRecord::Base has_many :people end ... and... class
My persons class allows a person to have a dict of numbers pointing to
Not sure how to call it, but say you have a class that looks
I have a class PersonList [XmlRoot(Persons)] PersonList : List<Human> when I serialize this to
I have a code file from the boto framework pasted below, all of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.