Build one-to-many association with ActiveRecord using alias for model name












0














I am building out a musician social site. Users can create bands, and they are considered from then on the manager of the band. I want to refer to a bands instance of a user as the manager.



Despite specifying the relationship in the models, and specifying class_name: "User" and foreign_key: "manager", the app will not save a new Band to the database.



user.rb



class User < ApplicationRecord
has_many :bands, foreign_key: 'manager'

validates_presence_of :name
validates_presence_of :password_digest
validates_presence_of :username
validates_uniqueness_of :username

has_secure_password

def admin?
self.role == "admin"
end
end


band.rb



class Band < ApplicationRecord
belongs_to :manager, class_name: 'User', foreign_key: 'manager'
validates_presence_of :name
validates_uniqueness_of :name
validates_presence_of :description
end


schema.rb



ActiveRecord::Schema.define(version: 2018_12_27_040935) do

create_table "bands", force: :cascade do |t|
t.string "name"
t.text "description"
t.integer "manager_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["manager_id"], name: "index_bands_on_manager_id"
end

create_table "users", force: :cascade do |t|
t.string "name"
t.string "username"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "role", default: "user"
end

end


seeds.rb



Edited seeds.rb



User.destroy_all
Band.destroy_all

user = User.new(name: 'John')
user.save
user = User.find_by_name('John')

user_band = user.bands.new(name: 'Band #1', description: 'My first band')

if user_band.save
puts "Done"
else
puts "Incomplete"
end


I run rails db:seed, and expect to see 'Done' in the console.
I get 'Incomplete'.



Testing in the rails console, the band instance has the error of "Manager must exist"



Also, a single user instance exists, with and id of 1.










share|improve this question





























    0














    I am building out a musician social site. Users can create bands, and they are considered from then on the manager of the band. I want to refer to a bands instance of a user as the manager.



    Despite specifying the relationship in the models, and specifying class_name: "User" and foreign_key: "manager", the app will not save a new Band to the database.



    user.rb



    class User < ApplicationRecord
    has_many :bands, foreign_key: 'manager'

    validates_presence_of :name
    validates_presence_of :password_digest
    validates_presence_of :username
    validates_uniqueness_of :username

    has_secure_password

    def admin?
    self.role == "admin"
    end
    end


    band.rb



    class Band < ApplicationRecord
    belongs_to :manager, class_name: 'User', foreign_key: 'manager'
    validates_presence_of :name
    validates_uniqueness_of :name
    validates_presence_of :description
    end


    schema.rb



    ActiveRecord::Schema.define(version: 2018_12_27_040935) do

    create_table "bands", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.integer "manager_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["manager_id"], name: "index_bands_on_manager_id"
    end

    create_table "users", force: :cascade do |t|
    t.string "name"
    t.string "username"
    t.string "password_digest"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "role", default: "user"
    end

    end


    seeds.rb



    Edited seeds.rb



    User.destroy_all
    Band.destroy_all

    user = User.new(name: 'John')
    user.save
    user = User.find_by_name('John')

    user_band = user.bands.new(name: 'Band #1', description: 'My first band')

    if user_band.save
    puts "Done"
    else
    puts "Incomplete"
    end


    I run rails db:seed, and expect to see 'Done' in the console.
    I get 'Incomplete'.



    Testing in the rails console, the band instance has the error of "Manager must exist"



    Also, a single user instance exists, with and id of 1.










    share|improve this question



























      0












      0








      0


      0





      I am building out a musician social site. Users can create bands, and they are considered from then on the manager of the band. I want to refer to a bands instance of a user as the manager.



      Despite specifying the relationship in the models, and specifying class_name: "User" and foreign_key: "manager", the app will not save a new Band to the database.



      user.rb



      class User < ApplicationRecord
      has_many :bands, foreign_key: 'manager'

      validates_presence_of :name
      validates_presence_of :password_digest
      validates_presence_of :username
      validates_uniqueness_of :username

      has_secure_password

      def admin?
      self.role == "admin"
      end
      end


      band.rb



      class Band < ApplicationRecord
      belongs_to :manager, class_name: 'User', foreign_key: 'manager'
      validates_presence_of :name
      validates_uniqueness_of :name
      validates_presence_of :description
      end


      schema.rb



      ActiveRecord::Schema.define(version: 2018_12_27_040935) do

      create_table "bands", force: :cascade do |t|
      t.string "name"
      t.text "description"
      t.integer "manager_id"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
      t.index ["manager_id"], name: "index_bands_on_manager_id"
      end

      create_table "users", force: :cascade do |t|
      t.string "name"
      t.string "username"
      t.string "password_digest"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
      t.string "role", default: "user"
      end

      end


      seeds.rb



      Edited seeds.rb



      User.destroy_all
      Band.destroy_all

      user = User.new(name: 'John')
      user.save
      user = User.find_by_name('John')

      user_band = user.bands.new(name: 'Band #1', description: 'My first band')

      if user_band.save
      puts "Done"
      else
      puts "Incomplete"
      end


      I run rails db:seed, and expect to see 'Done' in the console.
      I get 'Incomplete'.



      Testing in the rails console, the band instance has the error of "Manager must exist"



      Also, a single user instance exists, with and id of 1.










      share|improve this question















      I am building out a musician social site. Users can create bands, and they are considered from then on the manager of the band. I want to refer to a bands instance of a user as the manager.



      Despite specifying the relationship in the models, and specifying class_name: "User" and foreign_key: "manager", the app will not save a new Band to the database.



      user.rb



      class User < ApplicationRecord
      has_many :bands, foreign_key: 'manager'

      validates_presence_of :name
      validates_presence_of :password_digest
      validates_presence_of :username
      validates_uniqueness_of :username

      has_secure_password

      def admin?
      self.role == "admin"
      end
      end


      band.rb



      class Band < ApplicationRecord
      belongs_to :manager, class_name: 'User', foreign_key: 'manager'
      validates_presence_of :name
      validates_uniqueness_of :name
      validates_presence_of :description
      end


      schema.rb



      ActiveRecord::Schema.define(version: 2018_12_27_040935) do

      create_table "bands", force: :cascade do |t|
      t.string "name"
      t.text "description"
      t.integer "manager_id"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
      t.index ["manager_id"], name: "index_bands_on_manager_id"
      end

      create_table "users", force: :cascade do |t|
      t.string "name"
      t.string "username"
      t.string "password_digest"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
      t.string "role", default: "user"
      end

      end


      seeds.rb



      Edited seeds.rb



      User.destroy_all
      Band.destroy_all

      user = User.new(name: 'John')
      user.save
      user = User.find_by_name('John')

      user_band = user.bands.new(name: 'Band #1', description: 'My first band')

      if user_band.save
      puts "Done"
      else
      puts "Incomplete"
      end


      I run rails db:seed, and expect to see 'Done' in the console.
      I get 'Incomplete'.



      Testing in the rails console, the band instance has the error of "Manager must exist"



      Also, a single user instance exists, with and id of 1.







      ruby-on-rails ruby activerecord






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 28 '18 at 18:48

























      asked Dec 28 '18 at 2:42









      John Mark

      116




      116
























          3 Answers
          3






          active

          oldest

          votes


















          1














          Update associations as follows:



          band.rb



          belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'


          user.rb



          has_many :bands, foreign_key: 'manager_id'


          Before creating band, manager should be present in user table.



          manager = User.find(1)
          band = manager.bands.new(name: "Jams", description: "Hey, its our first band.")
          if band.save
          puts "Done"
          else
          puts "Incomplete"
          end





          share|improve this answer























          • Have tried, will not work. Receive errors. ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
            – John Mark
            Dec 28 '18 at 5:42










          • manager = User.find(1)...this will return manager and then create brand as manager.bands.new(name: 'My band')
            – Ni3
            Dec 28 '18 at 5:46












          • where are you saying that user = User.find(1) should go?
            – John Mark
            Dec 28 '18 at 5:48










          • Replace this with new queries: jms = Band.new(name: "Jams", description: "Hey, its our first band.", manager_id: 1)
            – Ni3
            Dec 28 '18 at 5:50






          • 1




            If your application is onboarding then reset database and run migrations again and seed also. Might be this will help.
            – Ni3
            Dec 28 '18 at 19:22



















          0














          band.rb



          belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'


          user.rb



          has_many :bands, foreign_key: 'manager_id'


          To test try this:



          manager = User.create(...user_information)
          jms = Band.new(name: "Jams", description: "Hey, its our first band.")
          jms.manager = manager
          jms.save


          If it fails, what is the output of jms.errors?






          share|improve this answer





















          • Thanks Keith. Get an error as follows. ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?))
            – John Mark
            Dec 28 '18 at 7:33



















          0














          Okay, so I changed my migration file, which apparently was causing the issue. The schema.rb looks identical, but the seeds.rb is now this…



          models



          user.rb
          has_many :bands, foreign_key: 'manager_id'



          band.rb
          belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'



          seeds.rb



          User.destroy_all
          Band.destroy_all

          user = User.new(name: 'John', username: "johnny", password: "nice")
          user.save
          user = User.find_by_name('John')

          user_band = Band.new(name: 'Band #1', description: 'My first band', manager_id: user.id)

          if user_band.save
          puts "Done"
          else
          puts "Incomplete"
          end


          The errors were: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)



          Changed migration file for bands



          Previously



          create_table :bands do |t|
          t.belongs_to :manager, foreign_key: true
          t.string :name
          t.text :description
          t.timestamps
          end


          Changed To



          create_table :bands do |t|
          t.belongs_to :manager
          t.string :name
          t.text :description
          t.timestamps
          end


          Removed option , foreign_key: true, Dropped DB, ran migrations, and now.



          It Works…






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953113%2fbuild-one-to-many-association-with-activerecord-using-alias-for-model-name%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Update associations as follows:



            band.rb



            belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'


            user.rb



            has_many :bands, foreign_key: 'manager_id'


            Before creating band, manager should be present in user table.



            manager = User.find(1)
            band = manager.bands.new(name: "Jams", description: "Hey, its our first band.")
            if band.save
            puts "Done"
            else
            puts "Incomplete"
            end





            share|improve this answer























            • Have tried, will not work. Receive errors. ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
              – John Mark
              Dec 28 '18 at 5:42










            • manager = User.find(1)...this will return manager and then create brand as manager.bands.new(name: 'My band')
              – Ni3
              Dec 28 '18 at 5:46












            • where are you saying that user = User.find(1) should go?
              – John Mark
              Dec 28 '18 at 5:48










            • Replace this with new queries: jms = Band.new(name: "Jams", description: "Hey, its our first band.", manager_id: 1)
              – Ni3
              Dec 28 '18 at 5:50






            • 1




              If your application is onboarding then reset database and run migrations again and seed also. Might be this will help.
              – Ni3
              Dec 28 '18 at 19:22
















            1














            Update associations as follows:



            band.rb



            belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'


            user.rb



            has_many :bands, foreign_key: 'manager_id'


            Before creating band, manager should be present in user table.



            manager = User.find(1)
            band = manager.bands.new(name: "Jams", description: "Hey, its our first band.")
            if band.save
            puts "Done"
            else
            puts "Incomplete"
            end





            share|improve this answer























            • Have tried, will not work. Receive errors. ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
              – John Mark
              Dec 28 '18 at 5:42










            • manager = User.find(1)...this will return manager and then create brand as manager.bands.new(name: 'My band')
              – Ni3
              Dec 28 '18 at 5:46












            • where are you saying that user = User.find(1) should go?
              – John Mark
              Dec 28 '18 at 5:48










            • Replace this with new queries: jms = Band.new(name: "Jams", description: "Hey, its our first band.", manager_id: 1)
              – Ni3
              Dec 28 '18 at 5:50






            • 1




              If your application is onboarding then reset database and run migrations again and seed also. Might be this will help.
              – Ni3
              Dec 28 '18 at 19:22














            1












            1








            1






            Update associations as follows:



            band.rb



            belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'


            user.rb



            has_many :bands, foreign_key: 'manager_id'


            Before creating band, manager should be present in user table.



            manager = User.find(1)
            band = manager.bands.new(name: "Jams", description: "Hey, its our first band.")
            if band.save
            puts "Done"
            else
            puts "Incomplete"
            end





            share|improve this answer














            Update associations as follows:



            band.rb



            belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'


            user.rb



            has_many :bands, foreign_key: 'manager_id'


            Before creating band, manager should be present in user table.



            manager = User.find(1)
            band = manager.bands.new(name: "Jams", description: "Hey, its our first band.")
            if band.save
            puts "Done"
            else
            puts "Incomplete"
            end






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 28 '18 at 8:37









            gaotongfei

            6815




            6815










            answered Dec 28 '18 at 5:39









            Ni3

            1768




            1768












            • Have tried, will not work. Receive errors. ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
              – John Mark
              Dec 28 '18 at 5:42










            • manager = User.find(1)...this will return manager and then create brand as manager.bands.new(name: 'My band')
              – Ni3
              Dec 28 '18 at 5:46












            • where are you saying that user = User.find(1) should go?
              – John Mark
              Dec 28 '18 at 5:48










            • Replace this with new queries: jms = Band.new(name: "Jams", description: "Hey, its our first band.", manager_id: 1)
              – Ni3
              Dec 28 '18 at 5:50






            • 1




              If your application is onboarding then reset database and run migrations again and seed also. Might be this will help.
              – Ni3
              Dec 28 '18 at 19:22


















            • Have tried, will not work. Receive errors. ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
              – John Mark
              Dec 28 '18 at 5:42










            • manager = User.find(1)...this will return manager and then create brand as manager.bands.new(name: 'My band')
              – Ni3
              Dec 28 '18 at 5:46












            • where are you saying that user = User.find(1) should go?
              – John Mark
              Dec 28 '18 at 5:48










            • Replace this with new queries: jms = Band.new(name: "Jams", description: "Hey, its our first band.", manager_id: 1)
              – Ni3
              Dec 28 '18 at 5:50






            • 1




              If your application is onboarding then reset database and run migrations again and seed also. Might be this will help.
              – Ni3
              Dec 28 '18 at 19:22
















            Have tried, will not work. Receive errors. ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
            – John Mark
            Dec 28 '18 at 5:42




            Have tried, will not work. Receive errors. ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
            – John Mark
            Dec 28 '18 at 5:42












            manager = User.find(1)...this will return manager and then create brand as manager.bands.new(name: 'My band')
            – Ni3
            Dec 28 '18 at 5:46






            manager = User.find(1)...this will return manager and then create brand as manager.bands.new(name: 'My band')
            – Ni3
            Dec 28 '18 at 5:46














            where are you saying that user = User.find(1) should go?
            – John Mark
            Dec 28 '18 at 5:48




            where are you saying that user = User.find(1) should go?
            – John Mark
            Dec 28 '18 at 5:48












            Replace this with new queries: jms = Band.new(name: "Jams", description: "Hey, its our first band.", manager_id: 1)
            – Ni3
            Dec 28 '18 at 5:50




            Replace this with new queries: jms = Band.new(name: "Jams", description: "Hey, its our first band.", manager_id: 1)
            – Ni3
            Dec 28 '18 at 5:50




            1




            1




            If your application is onboarding then reset database and run migrations again and seed also. Might be this will help.
            – Ni3
            Dec 28 '18 at 19:22




            If your application is onboarding then reset database and run migrations again and seed also. Might be this will help.
            – Ni3
            Dec 28 '18 at 19:22













            0














            band.rb



            belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'


            user.rb



            has_many :bands, foreign_key: 'manager_id'


            To test try this:



            manager = User.create(...user_information)
            jms = Band.new(name: "Jams", description: "Hey, its our first band.")
            jms.manager = manager
            jms.save


            If it fails, what is the output of jms.errors?






            share|improve this answer





















            • Thanks Keith. Get an error as follows. ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?))
              – John Mark
              Dec 28 '18 at 7:33
















            0














            band.rb



            belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'


            user.rb



            has_many :bands, foreign_key: 'manager_id'


            To test try this:



            manager = User.create(...user_information)
            jms = Band.new(name: "Jams", description: "Hey, its our first band.")
            jms.manager = manager
            jms.save


            If it fails, what is the output of jms.errors?






            share|improve this answer





















            • Thanks Keith. Get an error as follows. ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?))
              – John Mark
              Dec 28 '18 at 7:33














            0












            0








            0






            band.rb



            belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'


            user.rb



            has_many :bands, foreign_key: 'manager_id'


            To test try this:



            manager = User.create(...user_information)
            jms = Band.new(name: "Jams", description: "Hey, its our first band.")
            jms.manager = manager
            jms.save


            If it fails, what is the output of jms.errors?






            share|improve this answer












            band.rb



            belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'


            user.rb



            has_many :bands, foreign_key: 'manager_id'


            To test try this:



            manager = User.create(...user_information)
            jms = Band.new(name: "Jams", description: "Hey, its our first band.")
            jms.manager = manager
            jms.save


            If it fails, what is the output of jms.errors?







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 28 '18 at 7:21









            Keith Mattix

            359312




            359312












            • Thanks Keith. Get an error as follows. ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?))
              – John Mark
              Dec 28 '18 at 7:33


















            • Thanks Keith. Get an error as follows. ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?))
              – John Mark
              Dec 28 '18 at 7:33
















            Thanks Keith. Get an error as follows. ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?))
            – John Mark
            Dec 28 '18 at 7:33




            Thanks Keith. Get an error as follows. ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?))
            – John Mark
            Dec 28 '18 at 7:33











            0














            Okay, so I changed my migration file, which apparently was causing the issue. The schema.rb looks identical, but the seeds.rb is now this…



            models



            user.rb
            has_many :bands, foreign_key: 'manager_id'



            band.rb
            belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'



            seeds.rb



            User.destroy_all
            Band.destroy_all

            user = User.new(name: 'John', username: "johnny", password: "nice")
            user.save
            user = User.find_by_name('John')

            user_band = Band.new(name: 'Band #1', description: 'My first band', manager_id: user.id)

            if user_band.save
            puts "Done"
            else
            puts "Incomplete"
            end


            The errors were: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)



            Changed migration file for bands



            Previously



            create_table :bands do |t|
            t.belongs_to :manager, foreign_key: true
            t.string :name
            t.text :description
            t.timestamps
            end


            Changed To



            create_table :bands do |t|
            t.belongs_to :manager
            t.string :name
            t.text :description
            t.timestamps
            end


            Removed option , foreign_key: true, Dropped DB, ran migrations, and now.



            It Works…






            share|improve this answer


























              0














              Okay, so I changed my migration file, which apparently was causing the issue. The schema.rb looks identical, but the seeds.rb is now this…



              models



              user.rb
              has_many :bands, foreign_key: 'manager_id'



              band.rb
              belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'



              seeds.rb



              User.destroy_all
              Band.destroy_all

              user = User.new(name: 'John', username: "johnny", password: "nice")
              user.save
              user = User.find_by_name('John')

              user_band = Band.new(name: 'Band #1', description: 'My first band', manager_id: user.id)

              if user_band.save
              puts "Done"
              else
              puts "Incomplete"
              end


              The errors were: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)



              Changed migration file for bands



              Previously



              create_table :bands do |t|
              t.belongs_to :manager, foreign_key: true
              t.string :name
              t.text :description
              t.timestamps
              end


              Changed To



              create_table :bands do |t|
              t.belongs_to :manager
              t.string :name
              t.text :description
              t.timestamps
              end


              Removed option , foreign_key: true, Dropped DB, ran migrations, and now.



              It Works…






              share|improve this answer
























                0












                0








                0






                Okay, so I changed my migration file, which apparently was causing the issue. The schema.rb looks identical, but the seeds.rb is now this…



                models



                user.rb
                has_many :bands, foreign_key: 'manager_id'



                band.rb
                belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'



                seeds.rb



                User.destroy_all
                Band.destroy_all

                user = User.new(name: 'John', username: "johnny", password: "nice")
                user.save
                user = User.find_by_name('John')

                user_band = Band.new(name: 'Band #1', description: 'My first band', manager_id: user.id)

                if user_band.save
                puts "Done"
                else
                puts "Incomplete"
                end


                The errors were: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)



                Changed migration file for bands



                Previously



                create_table :bands do |t|
                t.belongs_to :manager, foreign_key: true
                t.string :name
                t.text :description
                t.timestamps
                end


                Changed To



                create_table :bands do |t|
                t.belongs_to :manager
                t.string :name
                t.text :description
                t.timestamps
                end


                Removed option , foreign_key: true, Dropped DB, ran migrations, and now.



                It Works…






                share|improve this answer












                Okay, so I changed my migration file, which apparently was causing the issue. The schema.rb looks identical, but the seeds.rb is now this…



                models



                user.rb
                has_many :bands, foreign_key: 'manager_id'



                band.rb
                belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'



                seeds.rb



                User.destroy_all
                Band.destroy_all

                user = User.new(name: 'John', username: "johnny", password: "nice")
                user.save
                user = User.find_by_name('John')

                user_band = Band.new(name: 'Band #1', description: 'My first band', manager_id: user.id)

                if user_band.save
                puts "Done"
                else
                puts "Incomplete"
                end


                The errors were: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)



                Changed migration file for bands



                Previously



                create_table :bands do |t|
                t.belongs_to :manager, foreign_key: true
                t.string :name
                t.text :description
                t.timestamps
                end


                Changed To



                create_table :bands do |t|
                t.belongs_to :manager
                t.string :name
                t.text :description
                t.timestamps
                end


                Removed option , foreign_key: true, Dropped DB, ran migrations, and now.



                It Works…







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 28 '18 at 19:22









                John Mark

                116




                116






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953113%2fbuild-one-to-many-association-with-activerecord-using-alias-for-model-name%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Mossoró

                    Error while reading .h5 file using the rhdf5 package in R

                    Pushsharp Apns notification error: 'InvalidToken'