authlogic icon indicating copy to clipboard operation
authlogic copied to clipboard

Rails 3 + scoping by subdomain error

Open tomekw opened this issue 16 years ago • 18 comments

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.

tomekw avatar May 12 '10 18:05 tomekw

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

jpartogi avatar Jul 01 '10 14:07 jpartogi

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

agileapplications avatar Jul 19 '10 21:07 agileapplications

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

tomekw avatar Jul 20 '10 07:07 tomekw

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

agileapplications avatar Jul 26 '10 09:07 agileapplications

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

rockrep avatar Sep 04 '10 21:09 rockrep

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

awd avatar Sep 09 '10 19:09 awd

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.

tsommer avatar Jan 14 '11 00:01 tsommer

@reload

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

phlegx avatar Jan 31 '11 22:01 phlegx

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

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

phlegx avatar Jan 31 '11 22:01 phlegx

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

osaris avatar Jul 18 '11 06:07 osaris

@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.

awd avatar Jul 19 '11 16:07 awd

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

osaris avatar Nov 22 '11 17:11 osaris

ping @releod to pull request again ?

osaris avatar Jan 13 '13 11:01 osaris

ping again ?

osaris avatar May 22 '14 19:05 osaris

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

awd avatar May 23 '14 03:05 awd

@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.

osaris avatar May 23 '14 06:05 osaris

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.

jaredbeck avatar Jul 12 '17 22:07 jaredbeck

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 avatar Aug 08 '17 02:08 jaredbeck