0

EDIT: This is not a dupe of Can't migrate after scaffold in Hartl's tutorial! I have Arel updated in my Gemfile just in case, but it makes no difference. Also, this is POST-migration, the rake finished just fine. However after the rake, when I try to query the association via the console, that is when I get the error!


I have two models with a simple one-to-many association between them. After adding the second model and raking the db, I opened rails console to test some stuff out, and I get an error every time I try to use the association in a query.

Here are the classes:

class Startup < ActiveRecord::Base
  has_many :reqs
end

class Req < ActiveRecord::Base
  belongs_to :startup
end

This is the migration for the Reqs table:

class CreateReqs < ActiveRecord::Migration
  def change
    create_table :reqs do |t|
      t.string :title
      t.text :desc
      t.integer :sort
      t.references :startup, index: true

      t.timestamps null: false
    end
  end
end

And here is the simple test I'm trying to test in the console afterwards:

> startup = Startup.first
> startup.reqs              ## Generates ArgumentError
> startup.reqs.build        ## Generates same error

And here is the beginning of the error:

ArgumentError: wrong number of arguments (1 for 0) from /usr/local/rvm/gems/ruby-2.1.4@rails4/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract_adapter.rb:271:in `initialize' from /usr/local/rvm/gems/ruby-2.1.4@rails4/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract_adapter.rb:271:in

I'm still getting the hang of a lot of the details of RoR, so please hang with me! But from what I thought I knew, this association seems so basic that I have no idea where to start fixing it. Thanks for your help!

Community
  • 1
  • 1
  • It's not a duplicate of that. No scaffolding and I did migrate just fine. It's after migration that this problem occurs when trying to utilize the has_many association. – Rachel Berry Nov 27 '14 at 09:57
  • 1
    [Please read careful, this is a Rails bug.](http://stackoverflow.com/questions/27139007/cant-migrate-database-after-scaffold-section-2-2-ruby-on-rails-tutorial-michae) – Roman Kiselenko Nov 27 '14 at 09:59
  • My Gemfile has been locked to the right version of Arel the entire time: gem 'arel', '6.0.0.beta2' Still getting the error, any ideas? – Rachel Berry Nov 27 '14 at 10:05
  • Did you run `bundle update arel` and restart your application server? – Marek Lipka Nov 27 '14 at 10:08
  • Yes, I did that much earlier on and have restarted the server several times between now and then :/ – Rachel Berry Nov 27 '14 at 10:10
  • What is the output of `bundle show arel`? – Marek Lipka Nov 27 '14 at 10:13
  • `$ bundle show arel /usr/local/rvm/gems/ruby-2.1.4@rails4/gems/arel-6.0.0.beta2` – Rachel Berry Nov 27 '14 at 10:14
  • I'm going to call it a night and look at this with fresh eyes in the morning. If you can think of anything else for me to try in the meantime, that would be great. Thanks! – Rachel Berry Nov 27 '14 at 10:17
  • It's quite strange, because the symptoms look exactly like in this other problem, which was fixed by updating `arel` to `6.0.0.beta2`. Anyway, I'm reopening this question. – Marek Lipka Nov 27 '14 at 10:20
  • just use/install previous Rails version. – Roman Kiselenko Nov 27 '14 at 10:53
  • Remove the null:false after timestamps in your migration file, you do not need it. Rails will handle this all alone. – Philipp Meissner Nov 27 '14 at 12:16
  • Someone suggested that before (and then deleted it).Rails generated it automatically in the migration file, which is odd. But I tried, and removing the null:false didn't make a difference. From now on I'll make sure it's not in my migrations though, thanks. – Rachel Berry Nov 27 '14 at 17:40

1 Answers1

0

I think this may have been some sort of fluke or bug in Rails. I tried again this morning and the rails console was still giving me the same error. Then just for kicks I tried adding another model that was the same, just with a different name:

class CreateSkills < ActiveRecord::Migration
  def change
    create_table :skills do |t|
      t.string :title
      t.text :desc
      t.integer :sort
      t.references :startup, index: true

      t.timestamps
    end
  end
end

After adding the has_many skills to Startup.rb, I went back into Rails console and now BOTH of the associations (Startup.skills and Startup.reqs) work just fine. Out of curiosity, I did a db:rollback and destroyed the Skill model and then went back into the console. Startup.reqs still works fine even though Skill is not there anymore.

I have no idea why this would make a difference, but wanted to post what worked for me in case someone else runs into a similar issue. Try generating another model to kind of "reset" the db and then you can roll it back and everything may work for you.