Thinking Sphinx search for different conditions from the same join table? - ruby-on-rails-3

This question is similar to
How do you search for different conditions from the same join table?, but I didn't get solution from this.z
My code
has_one :insurance_type_link, as: :enumerable, class_name: "Utility::UserDefinedEnumLink",
conditions: {user_defined_enum_links: {user_defined_enum_id: Utility::UserDefinedEnum.insurance_types.map(&:id)}}
has_one :payment_duration_link, as: :enumerable, class_name: "Utility::UserDefinedEnumLink",
conditions: {user_defined_enum_links: {user_defined_enum_id: Utility::UserDefinedEnum.payment_durations.map(&:id)}}
My define_index has this
indexes insurance_type_link.user_defined_enum.enum_value, as: :it, sortable: :insensitive
indexes payment_duration_link.user_defined_enum.enum_value, as: :pd, sortable: :insensitive
And sql query generated inside inside development.sphinx.conf is
AS `it_sort`, LOWER(IFNULL(`user_defined_enums_user_defined_enum_links`.`enum_value`, ''))
AS `pd_sort`, IFNULL(`user_defined_enums`.`enum_value`, '')
So it is generating different query to both the relation as a result only first query is giving me proper search results.
How to fix this ?
Thanks in advance.

I fixed it using a short cut method, not sure whether it is okay or not... I defined a third relation that include both the relations that I defined earlier only for the sake of searching
and used that in define_index method .

Related

Rails ActiveRecord Access association's(children?) latest created objects

as title said i am trying to access an array of objects of an association
This is a has_many association
here is my class
class Keyword < ApplicationRecord
has_many :rankings
end
class Ranking < ApplicationRercord
belongs_to :keyword
end
There are a attribute in ranking called position:integer, i want to be able to access all latest created rankings from all keyword here is what i got so far
Keyword.all.joins(:rankings).select( 'MAX(rankings.id) ').pluck(:created_at, :keyword_id, :position)
i've read some other post suggesting me to use MAX on rankings.id, but i am still not able to return the array
At the moment Keyword.count return 4597
Ranking.count return 9245
Each keyword has generated about 2 rankings, but i just want the latest ranking from each keyword in array format, so to get latest of each i should expect around 4597
Not sure if i explained clear enough, hope u guys can help me :'( thanks really appreciate it
If you are using Postgres. You can use DISTINCT ON
Keyword.joins(:rankings)
.select("DISTINCT ON(ratings.keyword_id) keywords.*, ratings.position, ratings.created_at AS rating_created_at")
.order("ratings.keyword_id, ratings.id DESC")
Now you can access position, rating_created_at
#keywords.each do |k|
k.position
....
#keywords.map { |k| [k.id, k.rating_created_at, k.position] }
If you have enough rankings you might want to store the latest ranking on the on keywords table as a read optimization:
class Keyword < ApplicationRecord
belongs_to :latest_ranking, class_name: :ranking
has_many :rankings, after_add: :set_latest_ranking
def set_latest_ranking(ranking)
self.update!(latest_ranking: ranking)
end
end
Keyword.joins(:latest_ranking)
.pluck(:created_at, :id, "rankings.position")
This makes it both very easy to join and highly performant. I learned this after dealing with an application that had a huge row count and trying every possible solution like lateral joins to improve the pretty dismal performance of the query.
The cost is an extra write query when creating the record.
Keyword.joins(:rankings).group("keywords.id").pluck("keywords.id", "MAX(rankings.id)")
This will give you an array which elements will include an ID of a keyword and an ID of the latest ranking, associated with that keyword.
If you need to fetch more information about rankings rather than id, you can do it like this:
last_rankings_ids_scope = Ranking.joins(:keyword).group("keywords.id").select("MAX(rankings.id)")
Ranking.where(id: last_rankings_ids_scope).pluck(:created_at, :keyword_id, :position)

Rails SQL Join two tables, One table has two columns with ids of other table, I need to get names of these ids

This is my model with associations:
class ProjectLineThreshold < ActiveRecord::Base
belongs_to :project_line
belongs_to :source, class_name: 'Language'
belongs_to :target, class_name: 'Language'
end
ProjectLineThreshold table has these columns (:id, :source_id, :target_id, :score). I need to add names of the languages by source_id and target_id from languages table.
I came up with this statement :
thresholds = self.project_line_thresholds.joins(:source, :target)
.select('project_line_thresholds.id, project_line_thresholds.source_id, project_line_thresholds.target_id,
project_line_thresholds.score, languages.name as source_name, languages.name as target_name')
But I get the same names for target and source. What would be the proper join statement, or I am doing it wrong?
You don't need the select statement, just get the names through the associations:
ProjectLineThreshold.includes(:source, :target).each do |plt|
puts "Source name: #{plt.source.name}"
puts "Target name: #{plt.target.name}"
end
Note that includes just makes sure to preload the associated records, otherwise it would run separate queries to retrieve source and target during each iteration of the loop.
The following query will only hit the db once:
self.project_line_thresholds
.joins(:source, :target)
.includes(:source, :target)
.map {|plt| [plt.id, plt.source_id, plt.target_id, plt.score, source.name, target.name]}
You need a statement only if you want to eager load your data, ie run one sql query for everything. Answers for this have already been provided.
Otherwise, you may just use the association methods provided by active record, for example:
#project_line_threshold.source.name
#project_line_threshold.target.name

ActiveRecord query with includes and conditions not giving expected SQL

Ok, so here's the issue I'm having. I have a model that has two relationships set on conditions in a through table.
has_one :link_resource, through: :resource_contexts, source: :resource, conditions: ['resource_contexts.question_id = ? ', -1]
has_many :sample_plans, through: :resource_contexts, source: :resource, conditions: ['resource_contexts.question_id = ? ', -2]
Then, in my controller I'm trying to get these included using
#funder_templates = FunderTemplate.find(:all, include: [:sample_plans, :link_resource], conditions: {active: true})
But for some reason, the sql comes out like this
ResourceContext Load (0.2ms) SELECT `resource_contexts`.* FROM `resource_contexts` WHERE (`resource_contexts`.funder_template_id IN (2,3,4,5,6,7,8,9,10,11,12,13,14,20,21,22,23,24,25,26,27,30,40) AND (resource_contexts.question_id = -2 ))
Notice it's only looking for the conditions of the first relationship, but not the second. I don't know if what I'm doing is just too complex for ActiveRecord to handle or if I'm just writing it incorrectly. Any help would be appreciated.
Rails 3.0.20
Ruby 1.9.2
Edit: To clarify, the really messed up part is #funder_templates.sample_plans is correct but #funder_templates.link_resource is sample plans as well! Without the includes, the relationships work fine, it's just not optimized.
Did you try ? #funder_templates = FunderTemplate.where(active: true).includes([:sample_plans, :link_resource])
I think, it should work

Refining the inner join when using includes in ActiveRecord

How do I add a condition to the ON clause generated by includes in active record while retaining eager loading?
Let's say I have these classes:
class Car
has_many :inspections
end
class Inspection
belongs_to :car
end
Now I can do:
Car.includes(:inspections)
Select * from cars LEFT OUTER JOIN inspections ON cars.id = inspections.car_id
But I want to generate this sql:
Select * from cars LEFT OUTER JOIN inspections ON cars.id = inspections.car_id
AND inspections.month = '2013-04-01'
(this doesn't work):
Car.includes(:inspections).where("inspections.month = 2013-04-01")
Select * from cars LEFT OUTER JOIN inspections ON cars.id = inspections.car_id
WHERE inspections.month = '2013-04-01'
I don't know this exactly, but what you are trying to do is probably not recommended i.e. violates one of Rails' conventions. According to this answer in a related question, the default behavior for such queries is to use two queries, like:
SELECT "cars".* FROM "cars";
SELECT "inspections".* FROM "inspections" WHERE "inspections"."car_id" IN (1, 2, 3, 4, 5);
This decision was made for performance reasons. That makes me guess that the exact type of query (JOIN or multiple queries) is an implementation detail that you cannot count on. Going along this train of thought, ActiveRecord::Relation probably wasn't designed for your use case, there is probably no way to add an ON condition in the query.
Going along this sequence of guesses, if you truly believe that your use case is unique, the best thing to do is probably for you to craft your own SQL query as follows:
Car.joins(sanitize_sql_array(["LEFT OUTER JOIN inspections ON inspections.car_id = cars.id AND inspections.month = ?", "2013-04-01"])
(Update: this was asked last year and did not receive a good answer.)
Alternative 1
As Carlos Drew suggested,
#cars = Cars.all
car_ids = #cars.map(&:id)
#inspections = Inspection.where(inspections: {month: '2013-04-01', car_id: car_ids})
# with scopes: Inspection.for_month('2013-04-01').where(car_id: car_ids)
However, in order to prevent car.inspections from triggering unnecessary SQL calls, you also need to do
# app/models/car.rb
has_many :inspections, inverse_of: :car
# app/models/inspection.rb
belongs_to :car, inverse_of: :inspections
Alternative 2
Perhaps you can find a way to cache the inspections for the current month, and then don't worry about eager loading. This might be the best solution, since the cache can be reused in various places.
#cars = Cars.all
#cars.each do |car|
car.inspections.where(month: '2013-04-01')
end
I've rethought your question more broadly. I think you are facing a code design problem as well as (instead of?) an ActiveRecord query problem.
You are asking to return a relation of Cars on which .inspections has been redefined to mean those Inspections matching a specific date. ActiveRecord does not allow you to redefine a model association on the fly, based on a query.
If you were not asking for a dynamic condition on the inspection date, I would tell you to use a has_many :through with a :condition.
has_many :passed_inspections, :through => :inspections, :conditions => {:passed => true}
#cars = Cars.includes(:passed_inspections)
Obviously, that would not work if you need to supply an inspection date on the fly.
So, in the end, I would tell you to do something like this:
#cars = Cars.all
#inspections = Inspection.where(inspections: {month: '2013-04-01', car_id: #cars.pluck(:id)})
(Exact, best implementation of that car_id where condition is up to debate. And you'll then need to group the #inspections by car_id to get the right subset in a given moment.)
Alternately, in a production environment, you might be able to rely on some fairly good/clever ActiveRecord caching. I'm not certain of this.
def inspections_dated(month)
inspections.where(month: month)
end
Car.includes(:inspections).each{|car| car.inspections_dated(month).each.etc. }
Alternately, Alternately
You can, through manual SQL, trick ActiveRecord into giving you extended Car objects with an unclear interface:
#cars_with_insp = Car.join("LEFT OUTER JOIN inspections ON inspections.car_id = cars.id AND inspections.month = '2013-04-01'").select("cars.*, inspections.*")
#cars_with_insp.each{|c| puts c.name; puts c.inspection_month}
You'll see, in that .each, that you have the inspection's attributes available directly on car, because you've convinced ActiveRecord with a join to return two records of one class as a single row. Rails will tell you its class is Car, but it's more than a Car. You'll either get each Car once, for no matching Inspections, or multiple times for each matching Inspection.
This should work:
Car.includes(:inspections).where( inspections: { month: '2013-04-01' })
The authors of Rails did not build this functionality into ActiveRecord, presumably because using WHERE returns the same result set, and they felt no need to have an alternative.
In the docs and code, we find the two "official" methods of adding conditions to included models.
In the actual source code: https://github.com/rails/rails/blob/5245648812733d2c31f251de3e05e78e68bfa3a5/activerecord/lib/active_record/relation/query_methods.rb we find them using WHERE to accomplish this:
And I quote: "
=== conditions
#
# If you want to add conditions to your included models you'll have
# to explicitly reference them. For example:
#
# User.includes(:posts).where('posts.name = ?', 'example')
#
# Will throw an error, but this will work:
#
# User.includes(:posts).where('posts.name = ?', 'example').references(:posts)
_END_QUOTE_
The docs mention another approach: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html under the header "Eager loading of associations"
QUOTE:
If you do want eager load only some members of an association it is usually more natural to include an association which has conditions defined on it:
class Post < ActiveRecord::Base
has_many :approved_comments, -> { where approved: true }, class_name: 'Comment'
end
Post.includes(:approved_comments)
This will load posts and eager load the approved_comments association, which contains only those comments that have been approved.
END QUOTE
You can technically use such an approach, but it in your case it may not be so useful if you are using dynamic month values.
These are the only options, which in any case return the same results as your AND based query.

Help with Rails find_by queries

Say if #news_writers is an array of records. I then want to use #news_writers to find all news items that are written by all the news writers contained in #news_writers.
So I want something like this (but this is syntactically incorrect):
#news = News.find_all_by_role_id(#news_writers.id)
Note that
class Role < ActiveRecord::Base
has_many :news
end
and
class News < ActiveRecord::Base
belongs_to :role
end
Like ennen, I'm unsure what relationships your models are supposed to have. But in general, you can find all models with a column value from a given set like this:
News.all(:conditions => {:role_id => #news_writers.map(&:id)})
This will create a SQL query with a where condition like:
WHERE role_id IN (1, 10, 13, ...)
where the integers are the ids of the #news_writers.
I'm not sure if I understand you - #news_writers is a collection of Role models? If that assumption is correct, your association appears to be backwards - if these represent authors of news items, shouldn't News belong_to Role (being the author)?
At any rate, I would assume the most direct approach would be to use an iterator over #news_writers, calling on the association for each news_writer (like news_writer.news) in turn and pushing it into a separate variable.
Edit: Daniel Lucraft's suggestion is a much more elegant solution than the above.