Rails 3 + scoping by subdomain error #135

Closed
tomekw opened this Issue May 12, 2010 · 18 comments

Comments

Projects
None yet
9 participants

tomekw commented May 12, 2010

Lets imagine Account model which has subdomain field and UserSession model which handles authentication. Acocunt model has "authenticates_many :user_sessions" in it.

Now I try to create new @user_session: @user_session = @current_account.user_sessions.new and I get: "undefined method `delete_if' for #Proc:0x55529b8"

I can provide more detailed report if necessary.

jpartogi commented Jul 1, 2010

I am also getting that error. I think this case is not working with Rails 3.

Me too. Is not an issue with rails I think - more with ruby 1.9

tomekw commented Jul 20, 2010

agileapplications: I think not. I got this error with REE.

tomekw: you might be right, problem should be in this method http://gist.github.com/490361

rockrep commented Sep 4, 2010

I was able to resolve this, although I have to admit the resolution looks like a major hack and I doubt this is the appropriate long-term solution. The problem is #scope returns a Proc in Rails 3.x, but a Hash in Rails 2.x, so my fix involved obtaining a suitable hash. If there is a better way to obtain this hash, I'd welcome the suggestion. You don't need to set :find_options in the call to authenticates_many with this approach as suggested in another thread on this issue. Monkeypatch authenticates_many as follows:

  def authenticates_many(name, options = {})
    options[:session_class]     ||= name.to_s.classify.constantize
    options[:relationship_name] ||= options[:session_class].klass_name.underscore.pluralize
    class_eval <<-"end_eval", __FILE__, __LINE__
      def #{name}
        relation_sql = #{options[:relationship_name]}.scoped.to_sql
        default_find_options = {:conditions => relation_sql[(relation_sql.rindex("WHERE")+5)..relation_sql.length].lstrip!}
        find_options = #{options[:find_options].inspect} || default_find_options
        find_options.delete_if { |key, value| ![:conditions, :include, :joins].include?(key.to_sym) || value.nil? }
        @#{name} ||= Authlogic::AuthenticatesMany::Association.new(#{options[:session_class]}, find_options, #{options[:scope_cookies] ? "self.class.model_name.underscore + '_' + self.send(self.class.primary_key).to_s" : "nil"})
      end
    end_eval
  end

awd commented Sep 9, 2010

I used rockreps solution in a forked repo. I submitted a pull request to Ben, and credited rockrep!

tsommer commented Jan 14, 2011

we fixed this by using a small class that returns the required find_options

class FindOptions
  def inspect
    '{ :conditions => ["`users`.portal_id = ?", id] }'
  end
end
authenticates_many :user_sessions, :find_options => FindOptions.new

Note that you have to replace 'portal_id' with the appropriate column for your user model.

phlegx commented Jan 31, 2011

@reload

Thx for your forked repo!!!! I test it now.

phlegx commented Jan 31, 2011

This forked repo has resolved all problems, bugs and deprecations in the branch rails3:

git://github.com/takuyan/authlogic.git

osaris commented Jul 18, 2011

@releod the bug is still there and your pull request isn't in the open requests. What happened ?

awd commented Jul 19, 2011

@osaris You can grab a forked repo here: https://github.com/releod/authlogic
Take a peek at the commits to see the fixes (I apologize I am not working on this forked repo actively; it was simply to keep our application running on Rails 3.0).

I believe my original pull request was removed, since it appeared this repo was becoming stale (aka: no response/commits from binarylogic). However, authlogic has been updated to work in Rails 3.1. Once our app upgrades to 3.1, if the bug persists, I'll consider opening another pull request. Also, Warden/Devise appear to be more active and to have more contributors as well.

osaris commented Nov 22, 2011

Issue isn't fixed in latest rails, maybe it's time to pull request again ?

osaris commented Jan 13, 2013

ping @releod to pull request again ?

osaris commented May 22, 2014

ping again ?

awd commented May 23, 2014

@osaris You've been pinging aimlessly for years, I think it's time to move on or write your own fix ;)

osaris commented May 23, 2014

@awd I'm using the @tsommer fix for years and it's working well but this should be integrated in authlogic, not in my app code.

Collaborator

jaredbeck commented Jul 12, 2017

Sorry for the delay. Is this is still a problem in the latest version of authlogic (currently 3.6.0)? I'd be happy to review a PR.

Collaborator

jaredbeck commented Aug 8, 2017

Closing due to inactivity. If anyone can reproduce this issue in the latest version of authlogic (currently 3.6.0) please let us know, thanks!

@jaredbeck jaredbeck closed this Aug 8, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment