I have the following models:
User
Ability
PricingRule
defined with the following relationships:
user has many pricing rules
ability has one pricing rule
The idea is to fetch all abilities matching some criteria and for each, fetch its pricing rule. However a custom pricing rule for a particular ability can be defined on a per user basis.
Currently I fetch all matching abilities and iterate on them to either:
try to find a current ability matching a user's pricing rule
or default to the ability's pricing rule
I am using Rails and ActiveRecord and here what I have so far:
user = User.first
Ability.all.map do |a|
user.pricing_rules.matching_ability(a).first || a.pricing_rule
end
Per user pricing rule customization should be done on demand by the business. The common workflow is to get the pricing rule from the abilities.
Any ideas or help to get me on the right track would be much appreciated.
EDIT:
Where the matching_ability implementation is as follow:
def self.matching_ability(ability)
where(name: ability.name)
end
You can "eager load" to avoid N+1 queries like so:
user = User.includes(pricing_rules: :abilities).first
Ability.includes(:pricing_rule).map do |a|
user.pricing_rules.matching_ability(a).first || a.pricing_rule
end
You should see in the SQL generated that this adds a LEFT OUTER JOIN to your queries, so ActiveRecord is loading the associated records in just the two queries. In particular, the user will be loaded with its pricing_rules and the abilities on each pricing_rule, and the abilities will be loaded with their pricing_rules.
However, implementing matching_ability using where may generate additional queries, returning you to the N+1 problem. To take advantage of the "eager load" in the first query, you may need to refactor to:
self.matching_ability(ability)
select{|a| a.name == ability.name}
end
Related
I have a user model that has one profile and also has one goal. The profile has a privacy field which is of type hstore, and in that field there can be a hash that indicates that it is OK to publicly display the goal, the hash looks like { 'show_goals' => '1' } if the goal can be displayed. I want a list of all goals that can be displayed publicly. My initial attempt was
def list_public
profiles = Profile.includes(:user).where("privacy #> hstore('show_goals','1')")
goals = []
profiles.each do |p|
goals << p.user.goals.first
end
goals
end
end
This works fine when there was a small number of users opting into allow their goals to be displayed, but is clearly not scaleable. Is there a single or a couple of ActiveRecord sql queries that can do the job of this code? I am using ruby on rails 5.1.
The easiest way is to also eager load the goals in your query:
profiles = Profile.includes(:user, user: :goals).where("privacy #> hstore('show_goals','1')")
This will produce one additional query fetching all the goals. You can then still use the remaining code as is, p.user.goals.first will not generate an additional database query.
I'm a Django beginner. I have two models that I would like to display as one table in the Django admin interface. Below are the simplified versions of the models:
class Final_Application_Personal_Detail(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=20)
id_no = models.CharField(max_length=14)
class Final_Application_Beneficiary_Detail(models.Model):
user = models.ForeignKey(User)
beneficiary_name = models.CharField(max_length=20)
beneficiary_id_no = models.CharField(max_length=14)
I suppose because these two models conceptually form part of one form (a Final_Application form), I could have maybe used Django's formwizard - however, because in reality both personal details and beneficiary details are quite a lot to fill in, I thought I'd want to give the user a chance to fill them in separately (in django formwizard, as far as I know, if the user doesn't fill out all the data at once, then all previous data is also lost because the form can't be saved with unvalidated fields).
I want to join the two models in Django admin so that the administrative user can see all a user's information (personal details and beneficiary's details) on one page. So, I'd like to do an inner join on the above-mentioned tables on the field 'user'. The SQL would look like this:
SELECT *
FROM Final_Application_Personal_Detail
JOIN Final_Application_Beneficiary_Detail
ON Final_Application_Personal_Detail.user = Final_Application_Beneficiary_Detail.user
With regards to Django's ORM, I've looked into the keyword argument related_to in order to join the tables. I don't think that will work, however...it seems as if 'select_related' is just another way to stipulate a foreign key. I now want to use raw SQL in order to join the tables. I've tried using the cursor function to implement raw SQL, but I don't quite know how to implement it so that the output shows in Django's admin interface. Also, I'm a little bit afraid of using raw SQL, because as far as I understand, raw SQL can introduce security risks (I've read that Django's ORM prevents SQL injections).
Thank you for your help.
You can't do this with django admin. However, you can use inlines to edit both models in the User admin.
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
class Final_Application_Personal_DetailInline(admin.StackedInline):
model = Final_Final_Applicationl_Detail
class Final_Application_Beneficiary_DetailInline(admin.StackedInline):
model = Final_Final_Application_Beneficiary_Detail
admin.site.unregister(User)
#admin.register(User)
class CustomUserAdmin(UserAdmin):
inlines = [
Final_Application_Personal_DetailInline,
Final_Application_Beneficiary_DetailInline,
]
I have problem with one query.
Let me explain what I want:
For the sake of bravity let's say that I have three tables:
-Offers
-Ratings
-Users
Now what I want to do is to create SQL query:
I want Offers to be listed with all its fields and additional temporary column that IS NOT storred anywhere called AverageUserScore.
This AverageUserScore is product of grabbing all offers, belonging to particular user and then grabbing all ratings belonging to these offers and then evaluating those ratings average - this average score is AverageUserScore.
To explain it even further, I need this query for Ruby on Rails application. In the browser inside application you can see all offers of other users , with AverageUserScore at the very end, as the last column.
Associations:
Offer has many ratings
Offer belongs to user
Rating belongs to offer
User has many offers
Assumptions made:
You actually have a numeric column (of any type that SQL's AVG is fine with) in your Rating model. I'm using a column ratings.rating in my examples.
AverageUserScore is unconventional, so average_user_score is better.
You don't mind not getting users that have no offers: average rating is not clearly defined for them anyway.
You don't deviate from Rails' conventions far enough to have a primary key other than id.
Displaying offers for each user is a straightforward task: in a loop of #users.each do |user|, you can do user.offers.each do |offer| and be set. The only problem here is that it will execute a separate query for every user. Not good.
The "fetching offers" part is a standard N+1 counter seen even in the guides.
#users = User.includes(:offers).all
The interesting part here is only getting the averages.
For that I'm going to use Arel. It's already part of Rails, ActiveRecord is built on top of it, so you don't need to install anything extra.
You should be able to do a join like this:
User.joins(offers: :ratings)
And this won't get you anything interesting (apart from filtering users that have no offers). Inside though, you'll get a huge set of every rating joined with its corresponding offer and that offer's user. Since we're taking averages per-user we need to group by users.id, effectively making one entry per one users.id value. That is, one per user. A list of users, yes!
Let's stop for a second and make some assignments to make Arel-related code prettier. In fact, we only need two:
users = User.arel_table
ratings = Rating.arel_table
Okay. So. We need to get a list of users (all fields), and for each user fetch an average value seen on his offers' ratings' rating field. So let's compose these SQL expressions:
# users.*
user_fields = users[Arel.star] # Arel.star is a portable SQL "wildcard"
# AVG(ratings.rating) AS average_user_score
average_user_score = ratings[:rating].average.as('average_user_score')
All set. Ready for the final query:
User.includes(:offers) # N+1 counteraction
.joins(offers: :ratings) # dat join
.select(user_fields, average_user_score) # fields we need
.group(users[:id]) # grouping to only get one row per user
In my application Users register for Events, which belong to a Stream. The registrations are managed in the Registration model, which have a boolean field called 'attended'.
I'm trying to generate a leaderboard and need to know: the total number of registrations for each user, as well as a count for user registrations in each individual event stream.
I'm trying this (in User.rb):
# returns an array of users and their attendence count
def self.attendance_counts
User.all(
:select => "users.*, sum(attended) as attendance_count",
:joins => 'left join `registrations` ON registrations.user_id = users.id',
:group => 'registrations.user_id',
:order => 'attendance_count DESC'
)
end
The generated SQL works for just returning the total attended count for each user when I run it in the database, but all that gets returned is the User record in Rails.
I'm about to give up and hardcode a counter_cache for each stream (they are fairly fixed) into the User table, which gets manually updated whenever the attended attribute changes on a Registration model save.
Still, I'm really curious as to how to perform a query like this. It must come up all the time when calculating statistics and reports on records with relationships.
Your time and consideration is much appreciated. Thanks in advance.
Firstly as a couple of points on style and rails functions to help you with building DB queries.
1) You're better writing this as a scope rather than a method i.e.
scope attendance_counts, select("users.*, sum(attended) as attendance_count").joins(:registrations).group('registrations.user_id').order('attendance_count DESC')
2) It's better not to call all/find/first on the query you've built up until you actually need it (i.e. in the controller or view). That way if you decide to implement action / fragment caching later on the DB query won't get called if the cached action / fragment is served to the user.
3) Rails has a series of functions to help with aggregating db data. for example if you only wanted a user's id and the sum of attended you could use something like the following code:
Registrations.group(:user_id).sum(:attended)
Other functions include count, avg, minimum, maximum
Finally in answer to your question, rails will create an attribute for you to access the value of any custom fields you have in the select part of your query. e.g.
#users = User.attendance_counts
#users[0].attendance_count # The attendance count for the first user returned by the query
I'm in a databases course and the instructor wants us to develop an e-commerce app. She said we can use any framework we like, and now that we're halfway through the semester she decided that Rails does too much and wants me to explicitly write my SQL queries.
So, what I'd like to do is to write my own functions and add them to the models to essentially duplicate already existing functionality (but with SQL that I wrote myself).
So the questions then become:
How do I execute manually created queries inside the model?
How do I stuff the results into an empty object that I can then return and work with inside the view?
Also, I'm aware of what terrible practice this is, I just don't want to start all over in PHP at this point.
I think, you should know 2-3 really necessary methods, to use it.
(assume we have at least 2 models, Order and User(customer for order))
For example, just to run query on your database use this:
Order.connection.execute("DELETE FROM orders WHERE id = '2')
to get number of objects from your database, the best way is use method "count_by_sql", it's scalable. I'm using it in my projects, where table has over 500 thousands records. All work to count application gives to database, and it did it much more efficient than app.
Order.count_by_sql("SELECT COUNT(DISTINCT o.user_id) FROM orders o")
this query gets number of all uniq users who has an order. we can "JOIN ON" tables, order results using "ORDER BY" and group results.
and the most often use method: find_by_sql
Order.find_by_sql("SELECT * FROM orders")
it returns to you an array with ruby objects.
Lets say you have a purchase
class Purchase < ActiveRecord:Base
def Purchase.find(id)
Purchase.find_by_sql(["Select * from purchases where id=?", id])
end
end
Maybe you want the products for a particular purchase. You can manually define the purchased_items in your Purchase model.
class Purchase < ActiveRecord:Base
def purchased_items
PurchasedItem.find_by_sql(["Select * from purchased_items where purchase_id=?",self.id])
end
end
So for example, in your controller where you now want to get the purchased items for a particular purchase you can now do this
#purchase = Purchase.find(params[:id])
#purchased_items = #purchase.purchased_items
If you need a more raw connection to the database, you can look into ActiveRecord:Base.connection.execute(sql)