Rails 3 + scoping by subdomain error
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.
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
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
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
I used rockreps solution in a forked repo. I submitted a pull request to Ben, and credited rockrep!
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.
@reload
Thx for your forked repo!!!! I test it now.
This forked repo has resolved all problems, bugs and deprecations in the branch rails3:
git://github.com/takuyan/authlogic.git
@releod the bug is still there and your pull request isn't in the open requests. What happened ?
@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.
Issue isn't fixed in latest rails, maybe it's time to pull request again ?
ping @releod to pull request again ?
ping again ?
@osaris You've been pinging aimlessly for years, I think it's time to move on or write your own fix ;)
@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.
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.
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!