SchemaPlus / schema_associations

ActiveRecord extension that automatically (DRY) creates associations based on the schema
Other
46 stars 8 forks source link

Rails 4 problems #7

Closed josemarluedke closed 11 years ago

josemarluedke commented 11 years ago

Hello guys!

I'm upgrading a Rails 3.2 to Rails 4 that is already using the schema_associations gem.

We are getting this error: undefined method_load_schema_associations_associations' for nil:NilClass`

The auto_create is configured to false on initializer file.

I believe the error happens here: https://github.com/lomba/schema_associations/blob/master/lib/schema_associations/active_record/associations.rb#L13

Can someone help to solve this problem?

ronen commented 11 years ago

hi!

schema_associations depends on schema_plus, which is currently in the process of being upgraded to rails 4; it should be ready any day now -- see lomba/schema_plus#115 and lomba/schema_plus#116.

once schema_plus is ready i'll try to take a look at schema_associations (unless you or somebody else beats me to it :)

if you want to get a jump on things, you could use the 'rails4' branch of schema_plus -- it should work fine at this point -- and then see whether/how schema_associations fails. if you do that, please post the results!

josemarluedke commented 11 years ago

HI!

We're already using the rails4 branch from schema_plus.

That was the only problem until now that we found on schema_plus and schema_associations. I tried to change the way that it is loaded but I could not find another way...

tovodeverett commented 11 years ago

I'll go spelunking and see what I can find!

tovodeverett commented 11 years ago

I created https://github.com/tovodeverett/schema_associations (look for the rails4 branch), but I didn't create a pull request because I think this should go against a rails4 branch in https://github.com/lombda/schema_associations.

That said, I couldn't reproduce the issue @josemarluedke reported. I was able to clean up some deprecation warnings and get the test suite running cleanly against Rails 4 in conjunction with the rails4 branch of schema_plus.

josemarluedke commented 11 years ago

Hello @tovodeverett!

I changed my Gemfile to use your repo, but the error still the same.

Just to remember that we are not using the auto_create option (it's configured to false). That means we need add 'schema_associations' on models to include the schema_associations.

tovodeverett commented 11 years ago

I've poked through the test suite, and it appears that it "should override auto_create positively implicitly" should be testing that exact scenario.

In the hopes that perhaps there was something in the test environment that was different, I went ahead and created a test environment:

rails new foobar --database=postgresql

I added gem 'schema_associations' to the Gemfile, uncommented the line for therubyracer, and ran bundle update. Note that since support for Rails 4 was just released in schema_plus 1.2.0, there was no need to specify a specific branch from GitHub.

I edited config/database.yml to support authentication with my development ID.

I then added config/initializers/schema_associations.rb with config.auto_create = false as detailed in the README.

Finally, I defined some models:

bundle exec rails generate scaffold Post
bundle exec rails generate scaffold Comment post:references
bundle exec rake db:create
bundle exec rake db:migrate
bundle exec rake db:test:prepare

At this point I verified that schema.rb had a foreign key in it (it did). I also noted that app/model/comment.rb had a belongs_to line, but that app/model/post.rb didn't have any associations. I verified that bundle exec rake test ran successfully. I deleted the belongs_to line from comment.rb and verified that the test suite complained about the missing association. I added schema_associations (without any parameters) to both the post.rb and comment.rb models files and verified that the test suite was now happy. Finally, I used rails c to verify that the associations were present in both directions.

I'm not sure what more to test. Can you get a full backtrace from where the error is occurring?

josemarluedke commented 11 years ago

Hi @tovodeverett!

First of all, thanks for your support and attention.

So, I have the project on my Github and it is open source: https://github.com/josemarluedke/catarse/tree/rails-4

Here is one of cases that is happening the problem, that apply one scope that is defined here.

I was debugging and found that the error happens only when I use the includes option.

tovodeverett commented 11 years ago

I've got catarse cloned and I've managed to get the test suite running, so I'll see what I can find as I poke around.

It did take me a little while to workaround the postgres extensions in 20121226120921_initial_schema.rb (my ID doesn't have superuser on my Postgres install, just createdb privs), but I found a solution (create the database, add the extensions using a superuser account, comment those lines out in the migration, then do the db:migrate).

I also had to add therubyracer and headless to the Gemfile (I don't have X running on my dev box), but I can now reproduce the error _load_schema_associations_associations error, so let me see what I can discover!

tovodeverett commented 11 years ago

I'm making progress - I can reproduce in my foobar demo, and I think I know where the issue is coming from. In https://github.com/rails/rails/blob/4-0-stable/activerecord/lib/active_record/associations/preloader.rb#L88, it's creating a new ActiveRecord::Relation using nil. That in turn calls https://github.com/rails/rails/blob/4-0-stable/activerecord/lib/active_record/relation/delegation.rb#L76, which allocates a new ActiveRecord::Relation object (since klass is nil - see what relation_class_for does 15 lines below) and then calls initialize on it, passing klass.

It looks like it may be as simple as defining a guard like so:

          klass.send :_load_schema_associations_associations unless klass.nil?

The current test suite passes with or without the guard, and it does appear to clean up the errors you're seeing in catarse (I'm still getting some other errors, but at least some of them appear to be related to issues creating the Postgres extensions on the test database when it gets dropped and recreated).

I'm going to do some more explanation, expand the test suite for schema_assocations to fail without the test and see if I can't figure out what won't work if I put that guard in place.

ronen commented 11 years ago

@tovodeverett's superhero work fixing this bug and tidying things up for rails 4 is now merged and released as v1.2.0. let us know if you still have any problems.

josemarluedke commented 11 years ago

The problem was solved!

Thanks for your help @tovodeverett and @ronen!