Ruby on Rails Active Records issue - sql

The models in question are:
User Followed
The "followeds" schema(or table) has "user_id" that is a reference to "users" table
And it has "followed_id" that is an integer.
What I want to achieve:
Inside my controller I want to select all the users that have an id corresponding to followed_id
Something like
def index
#users = User.all.where(something like => "user.id: followeds.followed_id")
end
Basically comparing the id for users table (user.id) to followeds.followed_id integers and if they match extract the record of the user.

In your User model you should have:
# No idea what's the nature of the relationship between your models, but
# pick one of these two
has_many :followeds, foreign_key: :followed_id
has_one :followed, foreign_key: :followed_id
then in your query call:
User.joins(:followeds) # or :followed if apply

Related

Active Records Query

I will go stright to the point:
I have these two models:
User
Followed
The Followed model has
user_id (the user that follows another user) and
followed_id, the user that is followed by the user with user_id.
the User model looks like this:
has_many :followeds, foreign_key: :followed_id, dependent: :destroy
the Followed model looks like this
belongs_to :user
What I want to achieve is this: I want to retrieve the followers for the current_user (the logged in user).
I have some idea on what I should do, which is something like this:
1 - join users table with followeds table
2 - select users where users.id = followeds.user_id (the user who follows the current_user)
3 - and (condition) followeds.followed_id = current_user.id (the user who is followed is the current_user, the one logged in)
I don' t know if it can help, but the following query is the one that I succesfully used (with the kind help of stack overflow users) to retrive the users that a user follows:
#users = User.joins(:followeds).where(followeds: {user_id: current_user.id})
Based on that I believe that the query should look something like
#users = User.joins(:followeds).where(followeds: {followed_id: current_user.id})
and then some query to select users.id = followeds.user_id
If I clear understand your associations
User.where(id: Followed.select(:user_id).where(followed_id: current_user.id))
It will generate something like this SQL
SELECT * FROM users
WHERE id IN (
SELECT user_id FROM followeds
WHERE followed_id = <current_user_id>
);
You can also add association to User model with :source and :through options
has_many :followers, through: :followeds, source: :user
And than just
current_user.followers
This will generate query like this
SELECT * FROM users
INNER JOIN followeds ON users.id = followeds.user_id
WHERE followeds.followed_id = <current_user_id>;

Adding eager loading to custom has_many relationships

Groups and users have has_many through relationships on the table user_roles, which specifies what role users have in each group (pending, member, admin, etc). For a given group, how might I return all users matching a particular role in that group such that the code is DRY and eager loads the appropriate associations?
class Group < ActiveRecord::Base
has_many :user_roles, dependent: :destroy
has_many :users, through: :user_roles
def members(role)
self.users.includes(:user_roles).where("user_role.role = ?", role)
# Returns following error message:
# PG::UndefinedTable: ERROR: missing FROM-clause entry for table "user_role"
end
Your table name probably 'user_roles' and not 'user_role'. Change the name of table in your where clause to 'user_roles'.
def members(role)
self.users.includes(:user_roles).where("user_roles.role = ?", role)
end

Sort records based on number of rows in another table - Rails

So I have a users table and in my relationship, I have defined that a user has many submissions and submissions belong to a user. I want to sort the users table based on how many submissions they have.
submission model
class Submission < ActiveRecord::Base
belongs_to :user
end
user model
class User < ActiveRecord::Base
has_many :submissions, dependent: :destroy
end
The far I have gone is I'm able to get how many submissions a user has using this query
Submission.all.count(:group => "user_id")
With this for example I'm able to get the number of submissions a user with a specific id has
{1=>3, 2=>5}
I want to have a sorted users table with the user with the highest number of submissions first. How can this be achieved in rails activerecord?
You can do what you want in 2 ways:
Using join, group by and order by count
User.select("COUNT(*) AS count_all, submissions.user_id AS submissions_user_id")
.joins('LEFT JOIN submissions ON submissions.user_id = users.id')
.group('submissions.user_id')
.order('COUNT(submissions.user_id) DESC')
This will generate the following sql:
SELECT COUNT(*) AS count_all, submissions.user_id AS submissions_user_id FROM "users" LEFT JOIN submissions ON submissions.user_id = users.id GROUP BY submissions.user_id ORDER BY COUNT(submissions.id) DESC
LEFT JOIN will get the users with 0 submissions too (if you have that situation)
Using counter_cache
The most efficient solutions for querying, in this context, is to use counter_cache
This will enable you to run a query like this:
User.order('submissions_count DESC')
which translates to:
SELECT * FROM users ORDER BY submissions_count DESC
!!! If you want to implement this, especially in production, do a backup of your database before starting. !!!
Read counter_cache docs to understand what it is and how it can help you.
Add a new column on users table named submissions_count.
class AddSubmissionsCountToUsers < ActiveRecord::Migration
def change
add_column :users, :submissions_count, :integer, default: 0
add_index :users, :submissions_count
end
end
Modify your Submission model and add counter_cache.
class Submission < ActiveRecord::Base
belongs_to :user, counter_cache: true
end
If you have a production database update submissions_count to reflect the number of existing submissions:
User.find_in_batches do |group|
group.each do |user|
user_submissions_count = Submission.where(user_id: user.id).count // find how many subscription a user has
user.update_column(:submissions_count, user_submissions_count)
end
end
Every time a user will create/destroy a subscription, submissions_count will be incremented/decremented for that user to reflect the change.

Rails - Delete rows from a non indexed table

I have a connection table that connects between 2 tables. It has 2 columns: user_id and course_id. The name of the table is: course_sub_managers. This table does not have an index. So, how do I delete all rows that meet a condition in which course_id = certain variable? As for now I use:
sql = "DELETE FROM course_sub_managers WHERE course_id = " + #course.id.to_s
ActiveRecord::Base.connection.execute(sql)
Is there a Rails way to write it?
I suspect you have an course model where you have written something like:
has_many :course_sub_managers
has_many :users, through => :course_sub_managers
in that case you can use:
#course.course_sub_managers.delete_all
It sounds you just want to handle the deletion of a course? Use the :dependent => :destroy on your relationships.
has_many :course_sub_managers, :dependent => :destroy
It will automatically remove the related table items when you destroy a course.

Is it possible to define a single SQL query that draws a set of permissible foreign_keys from one table and then uses them to filter another?

Specifically, I'm trying to figure out if it's possible to generate SQL that does what I want to feed into Ruby-on-Rails' find_by_sql method.
Imagine there are Users, who are joined cyclically to other Users by a join table Friendships. Each User has the ability to create Comments.
I'd like a SQL query to return the latest 100 comments created by any friends of a given user so I can display them all in one convenient place for the user to see.
This is tricky, since essentially I'm looking to filter the comments by whether their foreign keys for their author are contained in a set of keys obtained derived from the user's friends' primary keys.
Edit: Clarifying the setup. I'm not exactly sure how to write a schema definition, so I'll describe it in terms of Rails.
class User
has_many :friends, :through => :friendships
has_many :comments
end
class Friendship
belongs_to :user
belongs_to :friend, :class_name => "User", :foreign_key => "friend_id"
end
def Comment
has_one :User
end
It's not that tricky, you just use joins. To get just the comments you only need to join the Friendships table and the Comments table, but you probably also want some information from the Users table for the person who wrote the comment.
This would get the last 100 comments from people who are friends with the user with id 42:
select top 100 c.CommentId, c.CommentText, c.PostDate, u.Name
from Friendships f
inner join Users u on u.UserId = f.FriendUserId
inner join Comments c on c.UserId = u.UserId
where f.UserId = 42
order by c.PostDate desc