Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset pre-compilation fails when using Devise and named scope with lambda in User model #1373

Closed
ljfc opened this issue Oct 10, 2011 · 7 comments

Comments

@ljfc
Copy link

ljfc commented Oct 10, 2011

Apologies if this is not in fact a problem with Devise, but lib/devise.rb, lib/devise/mapping.rb and lib/devise/rails/routes.rb appear in the backtrace, so I thought it was worth letting you know.

The problem appeared when I added a named scope incorporating a lambda function to the User model that uses devise:
scope :find_by_skill_offered, lambda { |skill_id| joins(:skills).where("user_skills.offered = ? AND skills.id = ?", :true, skill_id) }

Now calling rake assets:precompile fails, both locally and on a development server, with the error Could not find table 'users'.

Full backtrace of the rake error here: https://gist.github.com/1276348

I have also asked about this on StackOverflow, as I can see that this may not be a bug in Devise, but I have tried a few workarounds elsewhere relating to other rake tasks failing in the same way, and a similar error relating to factory_girl, but none of them seem to work in this case.

http://stackoverflow.com/questions/7716900/rails-capistrano-deploy-error-fails-on-rake-assetprecompile-in-named-scope-wit

@josevalim
Copy link
Contributor

Can you please push to Github a small application that reproduces the error? Also, please include Devise and Rails versions in your reports and please ensure that you are running on latest Devise and Rails 3.1.

@ljfc
Copy link
Author

ljfc commented Oct 10, 2011

Apologies for the omissions José, I am relatively new to GitHub.

I have spent the past hour or so looking into this, and I have a few more pieces of information.

The application you wanted is here: https://github.com/ljfc/devise_error

Recreating the error should be just a matter of running rake assets:precompile

Indeed, the example will not even permit the initial rake db:migrate.

A few notes:

Adding the Heroku fix config.assets.initialize_on_precompile = false to config/application.rb, does not appear to help.

The problem was apparent in Devise 1.4.5, and upgrading to 1.4.8 has also not helped. The example app uses 1.4.8.

The example app uses Rails 3.1.1, and also displays the known Rack 1.3.4 warning already initialized constant WFKV_, which appears to be unrelated.

My original app is still using Rails 3.1.0 for the moment, but this error seems to occur with both versions.

I have included the same scope code in a similar small application using a simple has_secure_password approach rather than Devise, and it doesn't give the same errors, so it does seem to be something to do with the code Devise adds.

A final note:

I have fixed this in my main app by replacing the scope-with-lambda with a class method, which I believe is now best practice anyway. So I am not under pressure to fix this personally, although it would still be good to know if there is something obvious wrong.

Let me know if there is anything else I can provide, but please bear in mind I am on UK time, so will not respond until tomorrow now.

@josevalim
Copy link
Contributor

Ok, thank you very much for the app and the debugging you have done so far. Please keep me posted if you find anything else, I will take a look at your app hopefully later this week.

@matt-hh
Copy link

matt-hh commented Oct 12, 2011

hi,

https://github.com/ljfc/devise_error/blob/master/app/models/user.rb#L3
my recommendation: don't use ":find_by_..." as name for a scope, because scopes always find something. :)
choose a meaningful name like ":by_something" or ":by_skill" (your question on stackoverflow).

if you rename your scope ":find_by_something" in ":by_something", then it works.

hope this helps.

@ljfc
Copy link
Author

ljfc commented Oct 12, 2011

Hi Matt,

Thanks for the tip. I guess I just thought it was functionally equivalent to the generated find_by_... methods, so it ought to be named the same. I suppose it's more similar to a find_by_all_... because it returns an empty array if nothing is found.

Could this actually be causing the error? The lambda function does work, it just causes certain rake tasks to bomb.

Leo

@matt-hh
Copy link

matt-hh commented Oct 12, 2011

Sorry, I can't explain the exact technical cause of the error in your particular case.
This issue can be closed (not a devise issue).

Best, Matt

@ljfc
Copy link
Author

ljfc commented Oct 12, 2011

Okay, cheers Matt.

@ljfc ljfc closed this as completed Oct 12, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants