I have three models that look something like this:
class Bucket < ActiveRecord::Base
has_many :entries
end
class Entry < ActiveRecord::Base
belongs_to :submission
belongs_to :bucket
end
class Submission < ActiveRecord::Base
has_many :entries
belongs_to :user
end
class User < ActiveRecord::Base
has_many :submissions
end
When I retrieve a collection of entries doing something like:
#entries = Entry.find(:all,
:conditions => ['entries.bucket_id = ?', #bucket],
:include => :submission)
The performance is pretty quick although I get a large number of extra queries because the view uses the Submission.user object. However, if I add the user to the :include statement, the performance becomes terrible and it takes over a minute to return a total of 50 entries and submissions spread across 5 users. When I run the associated SQL commands, they complete in well under a second - the SQL query performance is the same from each set of queries.
#entries = Entry.find(:all,
:conditions => ['entries.bucket_id = ?', #bucket],
:include => {:submission => :user})
Why would this second command have such terrible performance compared to the first?
it's because you have a double join in second statement. So the number of result is bigger.
More bigger the result is much slow it's.
I'm not sure if the performance will be much better, but try:
#bucket.entries.find(:all, :include => {:submission => :user})
I'm really only familiar with MySQL, but if your database supports them, indexes will help performance significantly. I usually do this in my migrations like:
def self.up
create_table :entries do |t|
t.references :submission
t.references :bucket
# other fields...
end
add_index :entries, :submission_id
add_index :entries, :bucket_id
end
This ended up being a problem with the serialization/deserialization of the user model in the entire object graph. By caching relevant data on the Entry and Submission models we were able to avoid the lookup to User and saved a considerable amount of time.
Related
I'm working on a rails app where the associations between data change with time. I've created a model for the associations:
create_table :accounts_numbers do |t|
t.integer :number_id
t.integer :account_id
t.date :start_date
t.date :end_date
And, so far, I have a simple model
class Account < ActiveRecord::Base
has_and_belongs_to_many :numbers, :through => accounts_numbers
end
But instead of
#account.numbers
I need something like
#account.numbers_at(Date.new(2010,2,3))
I thought I could use :conditions, but I wouldn't haven't seen a way to tell has_and_belongs_to_many to create a parameterized field. I've also looked into named_scope, but that only seems to return accounts, not numbers.
More importantly, this pattern is going to cover many relationships in my code, so would there be a way to coin a time_dependent_has_and_belongs_to_many for use all over?
After much more searching, I finally found out what to do; In the /lib dir of my project, I created a module, TimeDependent:
module TimeDependent
def at(date)
find(:all, :conditions=>["start_date <= ? AND ? < end_date"], date, date)
end
end
So, my model becomes
require "TimeDependent"
class Account < ActiveRecord::Base
has_many :accounts_numbers
has_many :numbers, :through => :accounts_numbers, :extend => TimeDependent
end
Which allows me to do exactly what I want:
#numbers = #account.numbers.at(Date.new(2010,2,3));
Couldn't this be done by writing a function for Object?
class Object
def numbers_at(time)
start = time.to_date
end = time.advance(:days => 1).to_date
AccountNumber.join(:accounts, :numbers).where("time BETWEEN(?, ?)", start, end)
end
end
I have the usual polymorphic associations for comments:
class Book < ActiveRecord::Base
has_many :comments, :as => :commentable
end
class Article < ActiveRecord::Base
has_many :comments, :as => :commentable
end
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
I'd like to be able to define Book.recently_commented, and Article.recently_commented based on the created_at timestamp on the comments. Right now I'm looking at a pretty ugly find_by_SQL query to do this with nested selects. It seems as though there must be a better way to do it in Rails without resorting to SQL.
Any ideas? Thanks.
For what it's worth, here's the SQL:
select * from
(select books.*,comments.created_at as comment_date
from books inner join comments on books.id = comments.commentable_id
where comments.commentable_type='Book' order by comment_date desc) as p
group by id order by null;
Sometimes it's just best to add a field to the object of which you are commenting. Like maybe a commented_at field of datetime type. When a comment is made on an object, simply update that value.
While it is possible to use SQL to do it, The commented_at method may prove to be much more scalable.
Not sure what your method has looked like previously but I'd start with:
class Book < ActiveRecord::Base
def self.recently_commented
self.find(:all,
:include => :comments,
:conditions => ['comments.created_at > ?', 5.minutes.ago])
end
end
This should find all the books that have had a comment created on them in the last 5 minutes. (You might want to add a limit too).
I'd also be tempted to create a base class for this functionality to avoid repeating the code:
class Commentable < ActiveRecord::Base
self.abstract_class = true
has_many :comments, :as => :commentable
def self.recently_commented
self.find(:all,
:include => :comments,
:conditions => ['comments.created_at > ?', Time.now - 5.minutes])
end
end
class Book < Commentable
end
class Article < Commentable
end
Also, you might want to look at using a plugin to achieve this. E.g. acts_as_commentable.
I want to find a ordered list of runners by their results.
models
class Race < ActiveRecord::Base
has_many :runners, :dependent => :destroy
end
class Runner < ActiveRecord::Base
belongs_to :race
has_one :result, :dependent => :destroy
end
class Result < ActiveRecord::Base
belongs_to :runner
end
trying to use something like this
ordered_runners = race.runners.all(:include => :result, :order => 'results.position ASC')
position is their finishing position ie [1,2,3,4....]
but if a result is missing (nil) then the runner is not included. Is there a way to do this and return all runners?
cheers
Runners without Results are not included because :include only brings in the data minimizing the number of queries to avoid N+1 hits to the db. You want to do an outer :join to include all runners no matter if they have a result or not.
ordered_runners = race.runners.all(:joins => "left outer join results on runners.id = results.runner_id", :order => 'results.position ASC')
Check this code based on your migration column/table names and your database.
This should return the runners with a null result:
race.runners.all(:include => :result, :conditions => "results IS NULL", :order => 'results.position ASC')
I'm currently writing some intranet web application where people could submit to admins requests for adding different resources. The example requests would be:
installing programs, in this case user will select which program he wants installed
increasing quota, in this case user will just enter the amount of disk space he needs or maybe he will select the predefined quantities - 1GB, 10GB etc...
create new email alias, in this case user will just type the alias.
...
I was thinking about having just one model UserRequests with the reference to the sender and
two optional attributes one would be reference_id that would refefrence to other tables (for
example the Program that he wants installed) and another would be used for free type fields
like email alias or quota.
So my problem is that based on the type of the request the model should contain either:
reference to other table
integer data
string data
Based on the type of the request the given action should be taken - probably email alias
could be added from rails but the application on users computer will be installed by hand.
Does anyone had similar problem? Do you think using polymorphism for this kind of stuff is a good idea? Do you have any suggestions on how to organize data in the tables?
Single Table Inheritance! This way you can have each type of request have custom validations, while still having every request live in the same table.
class CreateUserRequests < ActiveRecord::Migration
def self.up
create_table :user_requests do |t|
t.string :string_data, :type
t.integer :user_id, :integer_data
t.timestamps
end
end
def self.down
drop_table :user_requests
end
end
class UserRequest < ActiveRecord::Base
belongs_to :user
end
class EmailAliasRequest < UserRequest
validates_presence_of :string_data
validates_format_of :string_data, :with => EMAIL_REGEX
end
class ProgramInstallRequest < UserRequest
belongs_to :program, :class_name => "Program", :foreign_key => "integer_data"
validates_presence_of :integer_data
end
class QuotaIncreaseRequest < UserRequest
validates_presence_of :string_data
validates_inclusion_of :string_data, :in => %w( 1GB 5GB 10GB 15GB )
end
And of course, alias your string_data and integer_data to email or whatnot to make your other code have a little more meaning. Let the model be the little black box that hides it all away.
I would use polymorphic associations, which let a model belong to more than one other model using a single association. Something like this:
class AdminRequest < ActiveRecord::Base
belongs_to :user
belongs_to :requestable, :polymorphic => true
end
class EmailAlias < ActiveRecord::Base
has_many :admin_requests, :as => :requestable
end
class ProgramInstall < ActiveRecord::Base
has_many :admin_requests, :as => :requestable
end
class QuotaIncrease < ActiveRecord::Base
has_many :admin_requests, :as => :requestable
end
As ever, Ryan Bates has an excellent Railscast on the subject.
I have two models, user and group. I also have a joining table groups_users.
I have an association in the group model:
has_many :groups_users
has_many :users, :through=> :groups_users
I would like to add pending_users which would be the same as the users association but contain some conditions. I wish to set it up as an association so that all the conditions are handled in the sql call. I know there's a way to have multiple accessors for the same model, even if the name is not related to what the table names actually are. Is it class_name?
Any help would be appreciated, thanks
Use named_scopes, they're your friend
Have you tried using a named_scope on the Group model?
Because everything is actually a proxy until you actually need the data,
you'll end up with a single query anyway if you do this:
class User < ActiveRecord::Base
named_scope :pending, :conditions => { :status => 'pending' }
and then:
a_group.users.pending
Confirmation
I ran the following code with an existing app of mine:
Feature.find(6).comments.published
It results in this query (ignoring the first query to get feature 6):
SELECT *
FROM `comments`
WHERE (`comments`.feature_id = 6)
AND ((`comments`.`status` = 'published') AND (`comments`.feature_id = 6))
ORDER BY created_at
And here's the relevant model code:
class Feature < ActiveRecord::Base
has_many :comments
class Comment < ActiveRecord::Base
belongs_to :feature
named_scope :published, :conditions => { :status => 'published' }
This should be pretty close - more on has_many.
has_many :pending_users,
:through => :groups_users,
:source => :users,
:conditions => {:pending => true}
:pending is probably called something else - however you determine your pending users. As a side note - usually when you see a user/group model the association is called membership.
In the User model:
named_scope :pending, :include => :groups_users, :conditions => ["group_users.pending = ?", true]
That's if you have a bool column named "pending" in the join table group_users.
Edit:
Btw, with this you can do stuff like:
Group.find(id).users.pending(:conditions => ["insert_sql_where_clause", arguments])