-
Prince John Wesley
ruby-on-rails-3 ruby-on-rails-3.1 scopes
I stumbled upon a wonderful article about scopes on Rails 3+ : http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly-named-scope/index.htmlYou can read there (in ‘Crazy Town’ section) that it’s possible to merge scopes from different models like this :class User < ActiveRecord::Basescope :published, lambda {joins(:posts).group(“users.id”) & Post.published} endwhich works just as expected, and allows you to do :User.published.to_sql #=> SELECT
-
Brendon Muir
ruby-on-rails-2 scopes
I’m upgrading from Rails 2.3.5 to 2.3.17 and have come across a pretty obscure problem. I use the following extensions to inject a scope within an association accessor, and provider a custom builder:module XYZFilterExtensiondef in_context(context)if proxy_owner.context == :abc && context != :adminscoped(:conditions => {:startup => false})elsescoped({})endendendmodule OtherExtensiondef build_with_component_instance(attributes = nil)attributes ||= {}attributes.reverse_merge!(:parent_
-
jklina
ruby-on-rails ruby-on-rails-3 meta-search scopes
In my Widget model I have the following:scope :accessible_to, lambda { |user|if user.has_role?(‘admin’)self.allelseroles = user.rolesrole_ids = []roles.each { |r| role_ids << r.id }self.joins(:widget_assignments).where(‘widget_assignments.role_id’ => role_ids)end }Ideally, I would like to use this scope as a filter for Ransack’s search results, so in my controller I have:def index@q = Widget.accessible_to(current_user).search(params[:q])@widgets = @q.result.order(‘created_at DESC’) endD
-
Stu
yii relation scopes default-scope
I can’t find too much documentation on applying a default scope to a model in yii, I was wondering if someone could explain or point me in the right direction.The quick version of my question:Is it possible to add a relation to a default scope, or to add a ‘with’ criteria by default to every AR search on a model?The long version of my question:A quick summary of my app:I have two models, provider and item. Which have a m:1 relationship where a provider can have many item’s, but each item can onl