Rails sort model on properties of a related model - sql

Given the following models:
team.rb
class Team < ActiveRecord::Base
has_many :events, :dependent => :destroy
has_many :challenges, :through => :events
validates :name, :presence => true, :uniqueness => true
end
challenge.rb
class Challenge < ActiveRecord::Base
has_many :events, :dependent => :destroy
has_many :teams, :through => :events
validates :name, :presence => true, :uniqueness => true
validates :flag, :presence => true, :uniqueness => true
end
event.rb
class Event < ActiveRecord::Base
belongs_to :team
belongs_to :challenge
validates :team, :presence => true
validates :challenge, :presence => true
end
I want to display the teams with the highest "rank".
Where the highest ranking team has the most challenges completed (events).
If there is a tie at X events then the team that completed the Xth event
first has the highest rank.
So I can easily sort the teams based on the number of events and then show them.
Like so:
def index
#teams = Team.includes(:events).
select("*, COUNT(events.id)").
group("teams.id, events.id").
order("COUNT(events.id) DESC")
end
However I don't know how to handle the case where there is a tie.
Does anybody know a good way of doing this with SQL?
I would rather use SQL to do this versus performing an extra step
on the app server.
Thanks!!

You could try this:
#teams = Team.includes(:events).
select("*, COUNT(events.id) AS event_count, MAX(events.created_at) AS last_event_created_at").
group("teams.id, events.id").
order("event_count DESC, last_event_created_at ASC")

So I got it working.
Thanks xnm. You sent me in the right direction. Can't believe I forgot about MAX. :(
I also had to fix my GROUP BY...
Here is the working version:
class Team < ActiveRecord::Base
...
def self.ranked
all(:select => "teams.*, COUNT(events.id) AS challenges_completed, MAX(events.created_at) AS last_event",
:joins => "LEFT OUTER JOIN events ON events.team_id = teams.id",
:group => "teams.id, events.team_id",
:order => "challenges_completed DESC, last_event ASC")
end
end

Related

has_many_and_belongs_to self-join association and conditional has_many

We have these 3 models:
class Group < ActiveRecord::Base
attr_accessible :name
has_many :users
has_and_belongs_to_many :suppliers,
:class_name => "Group",
:foreign_key => "customer_id",
:association_foreign_id => "supplier_id"
has_and_belongs_to_many :customers,
:class_name => "Group",
:foreign_key => "supplier_id",
:association_foreign_id => "customer_id"
has_many :orders, :as => :orderable
validates :name => :presence => true
end
class User < ActiveRecord::Base
attr_accessible :email, :name
has_many :orders, :as => :orderable
belongs_to :group
validates :email, :name, :group_id, :presence => true
end
class Order < ActiveRecord::Base
belongs_to :orderable, :polymorphic => true
validates :orderable_id, :presence => true
end
According to the self-joined Group model we have 2 "types" of groups: customers and suppliers.
Now we want that the has_many :orders, :as => :orderable association should exist only for the customer group type and not for suppliers.
So, only a customer could have many orders while a supplier cannot have any Order associated.
Is there a way to achieve this? Or I have to split the Group model into Customer and Supplier models?
Thanks!

How do I delete a record from all tables it is referred to?

Good morning fellow Overflowers,
Small problem with model associations. I have these model associations:
class Categorization < ActiveRecord::Base
belongs_to :exhibit
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :categorizations
has_many :exhibits, :through => :categorizations
acts_as_indexed :fields => [:title]
validates :title, :presence => true, :uniqueness => true
end
class Exhibit < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through => :categorizations, :source => :category
acts_as_indexed :fields => [:title, :bulb]
validates :title, :presence => true, :uniqueness => true
belongs_to :foto, :class_name => 'Image'
end
So, essentially Categorization ends up with these columns (date/time stamps omitted):
categorization_id, exhibit_id and category_id.
My problem is that when I delete an Exhibit, its reference on the Categorization table is not deleted thus getting a DB error on my view. I have to first unassign the Exhibit from any Category and then delete it safely. Or (given for example that the Exhibit I delete has :exhibit_id=>'1') when I give in the rails console: Categorization.find_by_exhibit_id(1).destroy
Thanks for any help!!
You can set the :dependent options on associations that you want Rails to follow when you delete their parents:
class Exhibit < ActiveRecord::Base
has_many :categorizations, :dependent => :destroy
...
end

Rails 3 - Restricting Article Tags

I am looking to remove any duplicated tags being displayed and have a maximum number of 10 tags on display on the index page. Any suggestions on how I might do this?
/controller/tags_controller
class TagsController < ApplicationController
def show
#tag = Tag.limit(10).all
#tag = Tag.find(params[:id])
#articles = #tag.articles
end
end
end
model/tag.rb
class Tag < ActiveRecord::Base
validates :name, :uniqueness => true
#default_scope :order => 'created_at DESC'
has_many :taggings, :dependent => :destroy
has_many :articles, :through => :taggings
end
To avoir duplicate and to order by published date, in your tag model :
validates :name, :uniqueness => true
default_scope :order => 'created_at DESC'
To fetch the ten first tags, in your controller :
#tags = Tag.limit(10).all
Voila!

rails complex many-to-many query

I've got 3 models: User, Team, and Membership -
class Team < ActiveRecord::Base
has_many :memberships
has_many :members, :through => :memberships, :source => :user
end
class User < ActiveRecord::Base
has_many :memberships, :dependent => :destroy
has_many :teams, :through => :memberships
def team_mates
teams = Team.where(:members => id)
team_mates = teams.members
end
end
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :team
validates :user_id, :presence => true
validates :team_id, :presence => true
end
And, I can't quite figure out how to write the team_mates method in the User model. It should return an array of the other users that are in a team with the current_user. My thought is that I should be useing a scope to limit Team to only include teams where the current user is a member but I can't quite figure out the syntax. Any help on this would be greatly appreciated.
Thanks!
Use the membership table to find users who share any team with the user you are calling the method on. Then to filter out duplicate users who share more than 1 team with current user, use distinct.
I haven't tested the below code, but hopefully it will get you on the right path:
def team_mates
m = Membership.scoped.table
Users.join(m).where(m[:team_id].in(team_ids)).project('distinct users.*')
end
UPDATE
Looks like some of the Arel methods in that answer don't have an easy mapping back to ActiveRecord land. (If someone knows how, I'd love to know!) And also it returns the 'current user'. Try this instead:
def team_mates
User.joins(:memberships).where('memberships.team_id' => team_ids).where(['users.id != ?', self.id]).select('distinct users.*')
end
How about?
User.joins(:memberships).joins(:teams).where("teams.id" => id).uniq
Maybe something like this
scope team_mates, lambda {|user_id, teams| joins(:memberships).where(:team_id => teams, :user_id => user_id)}
def team_mates
User.team_mates(self.id, self.teams.collect {|t| t.id})
end
Here is more info:
http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html

multiple joins using activerecord in rails

I'm building a small twitter style microblogging service where users can follow other users and get a feed of their messages
I have the following models:
class Follow < ActiveRecord::Base
belongs_to :follower, :class_name => "User"
belongs_to :followee, :class_name => "User"
end
class User < ActiveRecord::Base
has_many :follows, :foreign_key => 'follower_id',
:class_name => 'Follow'
has_many :followers, :through => :follows
has_many :followed, :foreign_key => 'followee_id',
:class_name => 'Follow'
has_many :followees, :through => :followed
has_many :messages
end
class Message < ActiveRecord::Base
belongs_to :user
end
To get a feed for the current user, I want to perform the following SQL query:
SELECT * FROM follows JOIN users JOIN messages WHERE follows.follower_id = current_user.id AND follows.followee_id = users.id AND users.id = messages.user_id;
What is the correct ActiveRecord way of doing this?
Not sure what you're looking for, but here is my suggestion:
I assume that you have other purposes for that Follow class, otherwise I don't see the purpose of it.
The "correct way" (i.e. my completely subjective way) to do it would actually be something like this:
class User < ActiveRecord::Base
has_and_belongs_to_many :followers, :foreign_key => 'followed_id',
:class_name => 'User', :association_foreign_key => 'follower_id',
:include => [:messages]
has_and_belongs_to_many :follows, :foreign_key => 'follower_id',
:class_name => 'User', :association_foreign_key => 'followed_id'
has_many :messages
end
class Message < ActiveRecord::Base
belongs_to :user
end
Then create the following table:
create_table :users_users, :id => false do |t|
t.integer :followed_id
t.integer :follower_id
end
And you're set:
followed = User.find :first
follower = User.find :last
followed.followers << follower
followed.followers.first.messages
followed.followers.first.followers.first.messages # etc...
But from what I make it, you want to show all the messages from all the followers at the same time.
This should be possible to achieve by adding
has_and_belongs_to_many :followed_messages, :foreign_key => 'follower_id',
:class_name => 'Message', :association_foreign_key => 'followed_id'
to the User class, but I don't know how correct that way would be. Or it might be possible to achieve with association extensions but there I can't really give any examples.
Update:
By changing the :class_name, it will associate it with the Message.id, didn't think about that so it will not be correct in this way.
So the only "nice" option is to go through the User class like in the first example.
The only other options I can see is either the association extensions (which I can't give you an example for) or perhaps using a finder statement.
has_many :followed_messages, :class_name => 'Message',
:finder_sql => 'select * from messages where user_id in(select followed_id from users_users where follower_id = #{id})'
You probably have to customize that sql statement to get everything to work, but at least you should get the picture :)
Keijro's arrangement would work better, though if you need the Follow table, then you can execute the SQL query you specified as follows:
Follow.all(:joins => { :messages, :users }, :conditions => { "follows.follower_id" => current_user.id, "follows.followee_id" => "users.id", "users.id" => "messages.user_id"} )