How to convert this sql joins/count statements in clean ruby ActiveRecord - sql

I'm trying to convert with no success my find_by_sql statement into a pure ActiveRecord query.
It is:
Corner.find_by_sql('SELECT corners.id, corners.name, count(members.*) FROM corners LEFT JOIN places ON corners.id = places.ubicacion_id LEFT JOIN members ON places.id = members.place_id GROUP BY corners.id,corners.name ORDER BY corners.name;')
nicely formatted, the sql expression would be:
SELECT corners.id,
corners.name,
count(members.*)
FROM corners
LEFT JOIN places ON corners.id = places.ubicacion_id
LEFT JOIN members ON places.id = members.place_id
GROUP BY corners.id,
corners.name
ORDER BY corners.name;
In very old versions of ActiveRecord, my approach would be using the find :all and then passing a hash of options, but this way is deprecated:
Corner.find( :all,
:joins => "LEFT JOIN places ON corners.id = places.ubicacion_id",
:joins => "LEFT JOIN members ON places.id = members.place_id",
:group => "corners.id,corners.name",
:order => "corners.name",
:select => "corners.id, corners.name, count(members.*)"
)
Which one would be the best approach to rewrite in the ActiveRecord way the query? This last snippet works well, but it makes no difference on using it rather than the plain sql one:
Corner.joins("LEFT JOIN places ON corners.id = places.ubicacion_id").joins("LEFT JOIN members ON places.id = members.place_id").group("corners.id,corners.name").order("corners.name").select("corners.id, corners.name, count(members.*)")
Many thanks!

It looks like you probably want to set up some model relationships to make this more ActiveRecord-like. You can find descriptions of how to do this in the Active Record Associations documentation.
Consider these relationships:
class Member < ActiveRecord::Base
belongs_to :place
end
class Place < ActiveRecord::Base
belongs_to :corner
has_many :members
end
class Corner < ActiveRecord::Base
has_many :places, foreign_key: "ubicacion_id"
end
Given those, you should be able to do something like this:
Corner.
select("corners.id, corners.name, count(members.*)").
joins(:places, :members).
group("corners.id, corners.name").
order("corners.name")
Each of the methods chained in the query will refine the query incrementally, much like building a native SQL statement. You can find the official documentation for these methods in the Active Record Query Interface

Related

How would I write this SQL query in Rails 3.2 syntax?

I have the following code my Track.rb model. Is there any way to write this using more Rails syntax? Im using Rails 3.2
#track = Track.find(7)
Submission.joins("LEFT JOIN missions ON missions.id = submissions.mission_id")
.joins("LEFT JOIN tracks ON tracks.id = missions.track_id")
.where("missions.track_id = ?", track.id)
Models:
Track.rb
has_many :missions
Mission.rb
belongs_to :track
has_many :submissions
Submission.rb
belongs_to :mission
First of all, do you really need left join for missions? You filter by missions.track_id, so you do not need submissions without missions. Inner join would be more appropriate in this case.
Next, why do you need to join tracks - you do not use this table in the next sql.
With these thoughts, you could rewrite your code as:
Submission.joins(:mission).where(missions: { track_id: track.id })

ActiveRecord .merge not working on two relations

I have the following models in my app:
class Company < ActiveRecord::Base
has_many :gallery_cards, dependent: :destroy
has_many :photos, through: :gallery_cards
has_many :direct_photos, class_name: 'Photo'
end
class Photo < ActiveRecord::Base
belongs_to :gallery_card
belongs_to :company
end
class GalleryCard < ActiveRecord::Base
belongs_to :company
has_many :photos
end
As you can see, Company has_many :photos, through: :gallery_cards and also has_many :photos. Photo has both a gallery_card_id and a company_id column.
What I want to be able to do is write a query like #company.photos that returns an ActiveRecord::Relation of all the company's photos. In my Company model, I currently have the method below, but that returns an array or ActiveRecord objects, rather than a relation.
def all_photos
photos + direct_photos
end
I've tried using the .merge() method (see below), but that returns an empty relation. I think the reason is because the conditions that are used to select #company.photos and #company.direct_photos are different. This SO post explains it in more detail.
#company = Company.find(params[:id])
photos = #company.photos
direct_photos = #company.direct_photos
direct_photos.merge(photos) = []
photos.merge(direct_photos) = []
I've also tried numerous combinations of .joins and .includes without success.
this might be a candidate for a raw SQL query, but my SQL skills are rather basic.
For what it's worth, I revisited this and came up (with help) another query that grabs everything in one shot, rather than building an array of ids for a second query. This also includes the other join tables:
Photo.joins("
LEFT OUTER JOIN companies ON photos.company_id = #{id}
LEFT OUTER JOIN gallery_cards ON gallery_cards.id = photos.gallery_card_id
LEFT OUTER JOIN quote_cards ON quote_cards.id = photos.quote_card_id
LEFT OUTER JOIN team_cards ON team_cards.id = photos.team_card_id
LEFT OUTER JOIN who_cards ON who_cards.id = photos.who_card_id
LEFT OUTER JOIN wild_cards ON wild_cards.id = photos.wild_card_id"
).where("photos.company_id = #{id}
OR gallery_cards.company_id = #{id}
OR quote_cards.company_id = #{id}
OR team_cards.company_id = #{id}
OR who_cards.company_id = #{id}
OR wild_cards.company_id = #{id}").uniq
ActiveRecord's merge returns the intersection not the union of the two queries – counterintuitively IMO.
To find the union, you need to use OR, for which ActiveRecord has poor built-in support. So I think you're correct that its best to write the conditions in SQL:
def all_photos
Photo.joins("LEFT OUTER JOIN gallery_cards ON gallery_cards.id = photos.gallery_card_id")
.where("photos.company_id = :id OR gallery_cards.company_id = :id", id: id)
end
ETA The query associates the gallery_cards to photos with a LEFT OUTER JOIN, which preserves those photo rows without associated gallery card rows. You can then query based on either photos columns or on associated gallery_cards columns – in this case, company_id from either table.
You can leverage ActiveRecord scope chaining to join and query from additional tables:
def all_photos
Photo.joins("LEFT OUTER JOIN gallery_cards ON gallery_cards.id = photos.gallery_card_id")
.joins("LEFT OUTER JOIN quote_cards ON quote_cards.id = photos.quote_card_id")
.where("photos.company_id = :id OR gallery_cards.company_id = :id OR quote_cards.company_id = :id", id: id)
end

Rails: where not in on has_many through

I have the following model
class Recipe < ActiveRecord::Base
has_many :recipe_allergens
has_many :allergens, through: :recipe_allergens
end
I'm trying to find all the recipes that do not have a given set of allergens, so I tried joins(:allergens).where.not(allergens: { id: allergens }).
Unfortunately, this does not account for the null case, where a Recipe may not have any associated Allergen.
I thought the LEFT OUTER JOIN that includes does would handle this.
How are you supposed to write this query?
EDIT:
I got this working with the following, though it seems really gross:
joins("LEFT OUTER JOIN recipe_allergens ON recipe_allergens.recipe_id = recipes.id")
.joins("LEFT OUTER JOIN allergens ON allergens.id = recipe_allergens.allergen_id")
.where(
Allergen.arel_table[:id].not_in(allergen_ids)
.or(Allergen.arel_table[:id].eq(nil))
)
Please tell me there's a better way!
I would do it this way:
allergenes = Allergene.where #....
allergene_ids = allergenes.pluck(:id)
bad_recipe_ids = RecipeAllergenes.where(allergene_id: allergene_ids)
.pluck(:recipe_id)
recipes = Recipe.where('id NOT IN ?', bad_recipe_ids)

Doing a join operation with a negative condition in Rails

I have two models:
class Member < ActiveRecord::Base
has_many :member_tags
end
and
class MemberTag < ActiveRecord::Base
belongs_to :member
# has a column 'tag'
end
I want to perform the following join:
Member.all(:joins=>:member_tags, :conditions=>"all members that don't have a member_tag with tag="hidden")
How do I do this? I guess it's more of an SQL question, than a rails one :)
I think this could do the trick:
select `members`.*
from `members`
LEFT JOIN `member_tags`
ON `members`.id = `member_tags`.member_id
where `members`.id NOT IN (select `members`.id
from `members`
LEFT JOIN `member_tags`
ON `members`.id = `member_tags`.member_id
where `member_tags`.tag = 'hidden'
);
I cant come up with anything better and this is an ugly method of doing it as it fires (n+1) sql queries for n members
Member.all.select {|member| !(member.member_tags.map(&:tag).include? "hidden")}

ActiveRecord::Relation joins with more conditions than just the foreign key

Is there any way to specify more than one conditions for a left outer join using ActiveRecord::Relation?
Take the following SQL statement for example. How can anyone rewrite this using ActiveRecord::Relation objects?
SELECT `texts`.*, `text_translations`.translation FROM `texts` LEFT OUTER JOIN `text_translations` ON `text_translations`.`id` = `texts`.`id` AND `text_translations`.`locale` = 'en'
Is there any way to do this under ActiveRecord 3.0.3+?
Thanks in advance.
first you should consider to use rails/activerecord conform relations. This means the foreign key in the text_translations table should be called text_id
Create your models and associations like this:
class Text < ActiveRecord::Base
# all possible translations!
has_many :text_translations
scope :with_translation_for, lambda { |lang| {
:select => "texts.*, tt.translation",
:joins => "LEFT OUTER JOIN text_translations AS tt ON tt.text_id = texts.id AND tt.locale = #{ActiveRecord::Base.sanitize(lang)}"
}}
# return nil if translation hasn't been loaded, otherwise you get a nasty NoMethod exception
def translation
read_attribute(:translation)
end
end
and
class TextTranslation < ActiveRecord::Base
# every translation belongs to a text
belongs_to :text
# define a scope for the language
scope :language, lambda { |lang| where(['locale = ?', lang]) }
end
How to use:
texts = Text.with_translation_for('en')
texts.each do |c_text|
unless c_text.translation.nil?
puts c_text.translation
else
puts "No translation available!"
end
end
Now to the pro and cons, the way using LEFT OUTER join will load you all texts even if there isn't a translation for a text in the desired language. The con is that you won't get the "TextTranslation" model object.
Anotherway is to load only the text which have the desired translation. You can do it like:
texts = Text.includes(:text_translations).where(:text_translations => {:locale => 'en'})
now texts[i].text_translations will return an array with all TextTranslations model object for this text matching the locale 'en'. But texts without a translation in the locale "en" won't show up.
Edit
Connected to your comment:
The problem about using .join(:tablename) on a relation is that, it will result in an INNER JOIN so this is not an option. You have to explicitly declare the LEFT join. Another thing is that if you use something like Text.includes(:text_translations).where(['text_translations.locale = ?', 'en']) the condition will be applied to the SQL query as whole and not on the possible LEFT join itself. What you actually can do is to declare associations like
has_many :english_translations, :class_name => 'TextTranslation', :conditions => ['locale = ?', 'en']
This way you can manage to load only english translations by eager loading (without any joins at all):
Text.includes(:english_translations).all
Checkt this out:
Ruby On Rails Guide about Joining Tables
ActiveRecord Association Docs, Search for LEFT OUTER JOIN