class Game < Event
has_many :statistics, :dependent => :destroy
...
end
class Statistic < ActiveRecord::Base
belongs_to :game
end
I want to make a named scope that only returns games that have no statistics.
Thanks
Try
named_scope :no_statistics, :include => :statistics, :conditions => ['statistics.id IS NULL']
Related
This associations worked OK in Rails 3, so for a User I could get UserQuest ordered by Quest.status. I can't find how to get this in Rails 4. Thanks for the help.
class Quest < ActiveRecord::Base
has_many :user_quests
end
class UserQuest < ActiveRecord::Base
belongs_to :user
belongs_to :quest
end
class User < ActiveRecord::Base
has_many :user_quests, :include => :quest, :order => "Quest.status DESC", :dependent => :delete_all
end
Try this:
class User < ActiveRecord::Base
has_many :user_quests,
-> { includes(:quest).order('quests.status DESC') },
:dependent => :delete_all
end
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
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)
Having set up my polymorphic relationship like so:
class Review < ActiveRecord::Base
belongs_to :reviewable, :polymorphic => true
belongs_to :user
end
class Wine < ActiveRecord::Base
has_many :reviews, :as => :reviewable
end
class Beer < ActiveRecord::Base
has_many :reviews, :as => :reviewable
end
I can do Wine.last.reviews and Beer.find(3).reviews etc...
What I'm strugling to do is go in the other direction, i.e. Lets say I want to find the last 10 reviews for Wine and the last 10 reviews for Beer.
The easiest way to do this is probably to add a named scope to your Review model that specifies the reviewable_type column.
Like so:
class Review < ActiveRecord::Base
belongs_to :reviewable, :polymorphic => true
belongs_to :user
named_scope :for_wines, :conditions => { :reviewable_type => 'Wine' }
named_scope :for_beers, :conditions => { :reviewable_type => 'Beer' }
end
That way you have the flexibility of scoping when finding your results...
Review.for_wines.approved.all
Review.for_beers.active.find(:all, :order => 'created_at')
etc
I am having great difficulty reading the API for named scopes. Each "bid" has a user_id and an auction_id. I need a scope to return the auctions the user has bid on.
Auction
class Auction < ActiveRecord::Base
has_many :bids
named_scope :current, lambda {
{:conditions => ["scheduled_start < ?", 0.minutes.ago],
:order => 'scheduled_start asc'}
}
named_scope :scheduled, lambda {
{:conditions => ["scheduled_start > ?", 0.minutes.ago],
:order => 'scheduled_start asc'}
}
end
Bid
class Bid < ActiveRecord::Base
belongs_to :user
belongs_to :auction
validates_numericality_of :point, :on => :create
#
# how do I write a named scope to check if a user has bid on an auction?
end
you might wanna try a has many through association instead of a named scope.
class User < ActiveRecord::Base
has_many :bids
has_many :auctions, :through => :bids
end
Or do it the other way round
class Auction < ActiveRecord::Base
has_many :bids
has_many :users, :through => :bids
end
That way, you can simply write: #auction.users.include?(user)
That is not very clear to read, so lets improve it:
class Auction < ActiveRecord::Base
has_many :bids
has_many :bidders, :through => :bids, :source => :user
end
And now: #auction.bidders.include?(user)
Lastly, you can pass more than one param to a lamda, so (not the best example)
named_scope :for_apple_and_banana, lambda{|apple_id, banana_id| {:conditions => ["apple_id = ? AND banana_id = ?", apple_id, banana_id ]}}