Rails: ActiveRecord query based on association value
You can perform a query like this:
Report.joins(:servers).where(:servers => {:company_id => 5})
To me, this is the cleaner solution to raw SQL.
Rails query records based on attribute of association
Something like this?
Pay.joins(:ee_pay => :company_pay).where(:company_pay => {:taxable => true})
Rails Active Record: Query by association value multiple iterations
i assume that you don't have any problem with colors params, that mean shoe_colors = params[:shoe][:colors].split('/')
ok, so your query should be
shoe_colors = params[:shoe][:colors].split('/')
@shoes = Shoe.joins(:colors)
.where('colors.name IN (?)', shoe_colors)
.group(:id).having(Arel.star.count.eq(shoe_colors.size))
Query a collection based on an association, and then return the whole association
You could write a custom join with aliased tables to match the search conditions:
def self.where_tagged_words_match_strings(search)
search.uniq!
joins("INNER JOIN tags mtags ON mtags.record_id = records.id INNER JOIN words mwords ON mwords.id = mtags.word_id")
.where("mwords.string IN (?)", search).group("records.id").having("COUNT(DISTINCT mwords.string) = #{search.length}")
end
ETA This query should select records with words matching an arbitrary search array. It does this by selecting records whose words match any of the strings in the array, then grouping by records' id, selecting only those having a number of matching strings equal to the number of strings queried.
You can then chain this with includes(:words)
to get all of the words associated, since the query above uses the aliased mwords
:
Record.where_tagged_words_match_strings(search).includes(:words)
Relatedly, while all of the above should work in SQLite, I highly recommend that you switch to a more powerful and production-ready SQL database such as MySQL or PostgreSQL.
Query on model associated as belongs_to in ActiveRecord (Is there a better way)
You can use LEFT OUTER JOIN
in this scenario.
Try:
InterestingGuest.join("LEFT OUTER JOIN users on users.user_id = interesting_guests.recommendee_id where users.membership_level = 'Guest'")
Here user_id
means recommendee_id
as per the association which you mentioned in the class.
Rails Active Record Association Query
Assuming:
- Channel
belongs_to
Group - You have a
@user
I believe you should be able to do:
Channel.where(group: @user.groups).pluck(:name)
Which should return an array
(which I assume is what you mean by list
) of the name
attribute of all Channel
s belonging to all Group
s which belong to @user
.
In your case (indicated in your comment to your original post which really should have been an edit), you should do:
<% @user_groups.each do |group| %>
<tr>
<td>
<%= group.name %>
</td>
<td>
<%= group.channels.pluck(:name) %>
</td>
</tr>
<% end %>
Related Topics
Restarting a Loop from the Top
How to Check If a Value Exists in an Array in Ruby
How to Match the Last Occurrence of a Pattern
How to Install a Gem or Update Rubygems If It Fails With a Permissions Error
Ruby: How to Post a File Via Http as Multipart/Form-Data
Why Is It Bad Style to 'Rescue Exception =≫ E' in Ruby
How to Prevent Browser Page Caching in Rails
How to Avoid Nomethoderror For Missing Elements in Nested Hashes, Without Repeated Nil Checks
How to Remove Rvm (Ruby Version Manager) from My System
How to Use the Conditional Operator (? :) in Ruby
Check If a String Contains Only Digits in Ruby