Ruby on Rails: Nested named scopes - ruby-on-rails

Is there any way to nest named scopes inside of each other from different models?
Example:
class Company
has_many :employees
named_scope :with_employees, :include => :employees
end
class Employee
belongs_to :company
belongs_to :spouse
named_scope :with_spouse, :include => :spouse
end
class Spouse
has_one :employee
end
Is there any nice way for me to find a company while including employees and spouses like this:
Company.with_employees.with_spouse.find(1)
or is it necessary for me to define another named_scope in Company:
:with_employees_and_spouse, :include => {:employees => :spouse}
In this contrived example, it's not too bad, but the nesting is much deeper in my application, and I'd like it if I didn't have to add un-DRY code redefining the include at each level of the nesting.

You can use default scoping
class Company
default_scope :include => :employees
has_many :employees
end
class Employee
default_scope :include => :spouse
belongs_to :company
belongs_to :spouse
end
class Spouse
has_one :employee
end
Then this should work. I have not tested it though.
Company.find(1) # includes => [:employee => :spouse]

You need define all the time all of your conditions. But you can define some method to combine some named_scope
class Company
has_many :employees
named_scope :with_employees, :include => :employees
named_scope :limit, :lambda{|l| :limit => l }
def with_employees_with_spouse
with_employees.with_spouse
end
def with_employees_with_spouse_and_limit_by(limit)
with_employees_with_spouse.limit(limit)
end
end
class Employee
belongs_to :company
belongs_to :spouse
named_scope :with_spouse, :include => :spouse
end
class Spouse
has_one :employee
end

try this
Company.with_employees.merge( Employees.with_spouse)

Related

Reusing activerecord scopes on has many through associations

Say I have a few activerecord models in my rails 3.1 project that look like this:
class Component < ActiveRecord::Base
has_many :bugs
end
class Bug < ActiveRecord::Base
belongs_to :component
belongs_to :project
scope :open, where(:open => true)
scope :closed, where(:open => false)
end
class Project < ActiveRecord::Base
has_many :bugs
has_many :components_with_bugs, :through => :bugs, :conditions => ["bugs.open = ?", true]
end
In Short: I have a has_many through association (components_with_bugs) where I want to scope the "through" model. At present I'm doing this by duplicating the code for the scope.
Is there any way to define this has many through association (components_with_bugs) such that I can reuse the Bug.open scope on the through model, while still loading the components in a single database query? (I'm imagining something like :conditions => Bug.open)
Rails 4 answer
Given you have:
class Component < ActiveRecord::Base
has_many :bugs
end
class Bug < ActiveRecord::Base
belongs_to :component
belongs_to :project
scope :open, ->{ where( open: true) }
scope :closed, ->{ where( open: false) }
end
You have two possibilities:
class Project < ActiveRecord::Base
has_many :bugs
# you can use an explicitly named scope
has_many :components_with_bugs, -> { merge( Bug.open ) }, through: :bugs, source: 'component'
# or you can define an association extension method
has_many :components, through: :bugs do
def with_open_bugs
merge( Bug.open )
end
end
end
Calling projet.components_with_bugs or project.components.with_open_bugs will fire the same sql query:
SELECT "components".* FROM "components"
INNER JOIN "bugs" ON "components"."id" = "bugs"."component_id"
WHERE "bugs"."project_id" = ? AND "bugs"."open" = 't' [["project_id", 1]]
Which one is better to use depends on your application. But if you need to use many scopes on the same association, I guess association extensions could be clearer.
The real magic is done with merge which allows you to, as the name says, merge conditions of another ActiveRecord::Relation. In this case, it is responsible for adding AND "bugs"."open" = 't' in the sql query.
Apart from your scopes , write the default scope as:
default_scope where(:open => true) in your "through" model Bug.
class Bug < ActiveRecord::Base
belongs_to :component
belongs_to :project
default_scope where(:open => true)
scope :open, where(:open => true)
scope :closed, where(:open => false)
end
And in the Project model remove :conditions => ["bugs.open = ?", true]
class Project < ActiveRecord::Base
has_many :bugs
has_many :components_with_bugs, :through => :bugs
end
I think the above will work for you.
Try using the following.
has_many :components_with_bugs, :through => :bugs do
Bug.open
end
Can't you use something like this ?
has_many :components_with_bugs, :through => :bugs, :conditions => Bug.open.where_values
I haven't tested it, just proposing an path for investigation
The http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html specifies
:conditions Specify the conditions that the associated object must meet in order to be included as a WHERE SQL fragment, such as authorized = 1.
Hence you can do it as:
class Project < ActiveRecord::Base
has_many :bugs
has_many :components_with_bugs, :through => :bugs do
def open
where("bugs.open = ?", true)
end
end
end
EDIT:
You can't specify another model's scope as a condition. In your case, they way you have it implemented is right. You can implement it another way as
has_many :components_with_bugs, :through => :bugs # in this case, no need to use the relation.
def open_bugs
self.bugs.openn # openn is the scope in bug. Don't use name 'open'. It's a private method of Array.
end

How do I create a scope that traverses a polymorphic association?

I have these models (psuedocode):
class Order
has_many :line_items
end
class LineItem
belongs_to :purchasable, :polymorphic => true
belongs_to :order
end
class Tile
has_one :line_item, :as => :purchasable
end
I want to make a scope that allows me to access tiles from an order. something like Order#tiles so that I can do things like this in controllers:
my_order.tiles.new(...)
my_order.tiles.find(params[:id]).update_attributes(...)
How can I construct such a scope? (or is there another technique I should use?)
The associations you have don't work together. I think you might be looking for something like this:
class Order
has_many :line_items
has_many :tiles, :through => :line_items, :source => :purchasable, :source_type => "Tile"
...
end
class LineItem
belongs_to :order
belongs_to :purchasable, :polymorphic => true
...
end
class Tile
has_many :line_items, :as => :purchasable
...
end

Using default_scope in a model, to filter by the correct instanceID

I have two tables:
books (id, name, desc, instance_id)
instances (id, domain)
A user should ONLY be able to see data that is assigned to their instance_id in records...
For the books, model, to accomplish this, I'm thinking about using a default scope.. Something like:
class Books < ActiveRecord::Base
attr_accessible :name, :description
belongs_to :user
default_scope :order => 'books.created_at DESC'
AND books.instance_id == current.user.instance_id
end
Any thoughts on that idea? Also how can I write that 2nd to last line for Rails 3? 'AND books.instance_id == current.user.instance_id'
Thanks
It's not a good idea to access the current user inside the model. I would implement this as follows:
class Instance < ActiveRecord::Base
has_many :users
has_many :books
end
class User < ActiveRecord::Base
belongs_to :instance
has_many :books, :order => "created_at DESC"
has_many :instance_books, :through => :instance, :source => :books,
:order => "created_at DESC"
end
class Book < ActiveRecord::Base
belongs_to :user
belongs_to :instance
end
List of Books associated with the user instance:
current_user.instance_books
List of Books created by the user:
current_user.books
Creating a new book:
current_user.books.create(:instance => current_user.instance, ..)
Note:
Your book creation syntax is wrong. The build method takes hash as parameter. You are passing two arguments instead of one.
user.books.build(params[:book].merge(:instance => current_user.instance}))
OR
user.books.build(params[:book].merge(:instance_id => current_user.instance_id}))

Rails named_scope across multiple tables

I'm trying to tidy up my code by using named_scopes in Rails 2.3.x but where I'm struggling with the has_many :through associations. I'm wondering if I'm putting the scopes in the wrong place...
Here's some pseudo code below. The problem is that the :accepted named scope is replicated twice... I could of course call :accepted something different but these are the statuses on the table and it seems wrong to call them something different. Can anyone shed light on whether I'm doing the following correctly or not?
I know Rails 3 is out but it's still in beta and it's a big project I'm doing so I can't use it in production yet.
class Person < ActiveRecord::Base
has_many :connections
has_many :contacts, :through => :connections
named_scope :accepted, :conditions => ["connections.status = ?", Connection::ACCEPTED]
# the :accepted named_scope is duplicated
named_scope :accepted, :conditions => ["memberships.status = ?", Membership::ACCEPTED]
end
class Group < ActiveRecord::Base
has_many :memberships
has_many :members, :through => :memberships
end
class Connection < ActiveRecord::Base
belongs_to :person
belongs_to :contact, :class_name => "Person", :foreign_key => "contact_id"
end
class Membership < ActiveRecord::Base
belongs_to :person
belongs_to :group
end
I'm trying to run something like person.contacts.accepted and group.members.accepted which are two different things. Shouldn't the named_scopes be in the Membership and Connection classes?
However if you try putting the named scopes in the Membership and Connection classes then you get this error (because Person.find(2).contacts returns an array of Persons which doesn't have an 'accepted' method:
>> Person.find(2).contacts.accepted
NoMethodError: undefined method `accepted' for #<Class:0x108641f28>
One solution is to just call the two different named scope something different in the Person class or even to create separate associations (ie. has_many :accepted_members and has_many :accepted_contacts) but it seems hackish and in reality I have many more than just accepted (ie. banned members, ignored connections, pending, requested etc etc)
You answered your own question:
Shouldn't the named_scopes be in the Membership and Connection classes?
Yes, they should be. This will let you call them as you wanted. It's also logically where they belong.
If you want something on Person that checks both, you can do something like:
named_scope :accepted, :conditions => ["connections.status = ? OR memberships.status = ?", Connection::ACCEPTED, Membership::ACCEPTED]
Maybe you want this to be an AND? not sure.
I'm sure this is not the best way and I believe you can do this on person and group models, but I also believe the following will work for you:
# models
class Person < ActiveRecord::Base
has_many :connections
has_many :contacts, :through => :connections
has_many :memberships
has_many :groups, :through => :memberships
end
class Group < ActiveRecord::Base
has_many :memberships
has_many :members, :through => :memberships
end
class Connection < ActiveRecord::Base
belongs_to :person
belongs_to :contact, :class_name => "Person", :foreign_key => "contact_id"
named_scope :accepted, :conditions => ["status = ?", Connection::ACCEPTED]
end
class Membership < ActiveRecord::Base
belongs_to :person
belongs_to :group
named_scope :accepted, :conditions => ["status = ?", Membership::ACCEPTED]
end
# controller
# get person's accepted contacts
#person = Person.first
#person.connections.accepted.map(&:contact)
# get group's accepted members
#group = Group.first
#group.memberships.accepted.map(&:person)

Eager loading a named_scope from an association in Rails

Is there any way to eager load a named_scope from an association?
I have my Article model:
class Article < ActiveRecord::Base
has_many :comments
end
and my Comment model:
class Comment < ActiveRecord::Base
belongs_to :article
named_scope :approved, :conditions => { :approved => true }
named_scope :unapproved, :conditions => { :approved => false }
end
I could eager load all comments for an article with:
#article = Article.find(params[:id], :include => :comments)
How can I do the same, but only for approved comments?
It is not built-in to rails at this time, but Ryan Daigle created a plugin called utility_scopes that adds a with() scope so you can do:
Article.approved.with(:comments)
Blog Post: http://ryandaigle.com/articles/2008/8/20/named-scope-it-s-not-just-for-conditions-ya-know
Github Repo: http://github.com/yfactorial/utility_scopes
[Updated per comment]
My bad. I didn't read well enough. I don't think there's anything that will let you call on an association named_scope like that. The closest thing I can think of would be creating a named_scope on Article:
class Article < ActiveRecord::Base
named_scope :with_approved_comments, {:include => :comments, :conditions => ['comments.approved = ?', true]}
end
Hope this helps.
The other answer didn't work for me since I have to load many associations and associations of those associations on some models. I found I had two options, change the association to have conditions and thus make an association for each scope I wanted or turn them into methods on the class that had the has_many association. I needed to do something along the lines of:
#newspaper = Newspaper.find params[:id], :include => {
:articles => {
:author => true,
:source => true
}
}
So in my example I changed:
class Newspaper < ActiveRecord::Base
has_many :articles
end
class Article
belongs_to :newspaper
belongs_to :author
has_one :source
named_scope :private, :conditions => { :private => true }
named_scope :public, :conditions => { :private => false }
end
to:
class Newspaper < ActiveRecord::Base
has_many :articles
def articles_public
articles.reject &:private
end
def articles_private
articles.select &:private
end
end
class Article
belongs_to :newspaper
belongs_to :author
has_one :source
end
I found this way to be preferable since I can now eager load articles when finding a newspaper and just change all article.private to article_private. Had I created two associations like:
has_many :articles_private, :class_name => 'Article', :conditions {:private => true}
has_many :articles_public, :class_name => 'Article', :conditions {:private => false}
I would have to eager load them both in cases where I needed all associated articles.

Resources