Association not working - ruby-on-rails

I have three models:
Department
class Department < ActiveRecord::Base
has_many :patients, :dependent => :destroy
has_many :waitingrooms, :dependent => :destroy
end
Waitingroom with fields patient_id:integer and department_id:integer
class Waitingroom < ActiveRecord::Base
belongs_to :patient
end
Patient with department_id:integer
class Patient < ActiveRecord::Base
belongs_to :department
has_many :waitingrooms
end
I save a waitingroom after a patient was in the waitingroom! So now i tried to retrieve the patients who where in the the waitingroom of the department:
def index
#waited = #current_department.waitingrooms.patients
end
Somehow it didnt worked it returned this error:
undefined method `patients' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Waitingroom:0x374c658>
But this worked: What did i wrong? Thanks!
def index
#waited = #current_department.waitingrooms
end

You can't invoke an association on a collection. You need to invoke it on a specific record. If you want to get all the patients for a set of waiting rooms, you need to do this:
def index
rooms = #current_department.waitingrooms
#waited = rooms.map { |r| r.patients }
end
If you want a flat array, you could (as a naive first pass) use rooms.map { |r| r.patients }.flatten.uniq. A better attempt would just build a list of patient ids and fetch patients once:
#waited = Patient.where(id: rooms.pluck(:patient_id).uniq)

Related

How to eliminate N+1 queries from database query in Ruby on Rails?

In my Rails 6 app I have these models:
class User < ApplicationRecord
has_many :read_news_items
has_many :news_items, :through => :read_news_items
end
class NewsItem < ApplicationRecord
has_many :read_news_items
has_many :users, :through => :read_news_items
def read?(user)
read_news_items.where(:user_id => user.id).any?
end
end
class ReadNewsItem < ApplicationRecord
belongs_to :user
belongs_to :news_item
end
In my controller action I want to list all news items and highlight the ones that have not yet been read by the user:
class NewsItemsController < ApplicationController
def index
#news_items = NewsItem.all
end
end
The problem is that this generates N+1 queries for each record because the read?(current_user) gets called for each user record.
How can this problem be overcome?
I tried appending includes(:read_news_items) and joins(:read_news_items) to the database query in my controller but to no avail.
You could try:
class NewsItem < ApplicationRecord
has_many :read_news_items
def read?(user)
if read_news_items.loaded?
read_news_items.any? {|rni| rni.user_id == user.id }
else
read_news_items.where(:user_id => user.id).any?
end
end
end
class NewsItemsController < ApplicationController
def index
#news_items = NewsItem.includes(:read_news_items).all
end
end
OK, I learned something from every answer that was given here. So thanks for that.
I changed my read? method to the following which seems to have eliminated the N+1 queries:
class NewsItem < ApplicationRecord
def read?(user)
user.read_news_items.pluck(:news_item_id).include?(id)
end
end

Association in rails Model

Below is the model Structure.
class UserFamilyRole < ActiveRecord::Base
belongs_to :user_family
belongs_to :user_role
belongs_to :organization
end
class UserFamily < ActiveRecord::Base
has_many :user_family_roles
has_many :user_roles
end
I am looking for below result from the above UserFamilyRole table.
{
user_family1: [user_role1, user_role2],
user_family2: [user_role1, user_role2, user_role3]
...
}
I achieved this Result from below query:
user_family_roles = UserFamilyRole.where(:organization_id => org_id)
results = {}
juser_family_roles.each do |user_family_role|
user_family = UserFamily.find(user_family_role.user_family_id)
result[user_family.name] = user_family.user_roles.collect(&:title)
end
puts results
But Above query doesn't look optimized one so can anyone help me to write the optimized query.
In our App org_id is present.

How to implement dynamic class_name for the association has_many? For the same table, different engines

Please tell me the way how to implement dynamic associative link, which is itself determined by the attribute model.
I have two engines(Tbitcoin, Tstripe) each of them have a table payment. The model User has pay_currency attribute, which is the managing.
class User < ActiveRecord::Base
has_many :payments, ~> { where "pay_currency = 'real'" } , class_name: Tstripe::Payment, foreign_key: :uid
has_many :payments, ~> { where "pay_currency = 'bitcoin'" } ,class_name: Tbitcoin::Payment, foreign_key: :uid
end
What are the ways to dynamically determine the engine using User.last.payments.create ?
I think that you need a regular method instead of has_many association which will find proper payments associated with the user according to pay_currency value. Example:
class User < ActiveRecord::Base
def payments
payment_class = case pay_currency
when "real"
Tstripe::Payment
when "bitcoin"
Tbitcoin::Payment
end
payment_class.for_user(self)
end
end
class Tstripe::Payment < ActiveRecord::Base
belongs_to :user
def self.for_user(user)
where(user_id: user.uid)
end
end

Complex ActiveRecord Query

I'm having a hard time wrapping my head around how to build this query. I'm not sure if I should try to create a bunch of scopes and try to chain them. Or do I put it into a class method? Or would I do a combo of both? If anyone could give me a short example it would keep me from jumping out the window, I've been working on this for over a week now.
class CensusDetail < ActiveRecord::Base
belongs_to :apartment
belongs_to :resident
end
class Apartment < ActiveRecord::Base
belongs_to :apartment_type
has_many :census_details
has_many :residents, :through => :census_details
end
class ApartmentType < ActiveRecord::Base
has_many :apartments
end
class Resident < ActiveRecord::Base
has_many :census_details
has_many :apartments, :through => :census_details
end
apartment.rent_ready = boolean value if apartment is ready
apartment_type.occupany = the number of allowed occupants in an apartment
census_detail.status = either "past", "present", or "new"
census_detail.moveout_date = date time resident is scheduled to move out
I need to build a query that does the following:
- if the apartment is rent ready then do the following:
- pull a list from census_details of all residents set as "current" for each apartment
-if the # of residents is less than the value of apartment_type.occupancy for this
apartment, then list it as available
-if the # of residents is = to the value of apartment_type.occupancy then
-does any resident have a scheduled move out date
-if not, do not list the apartment
-if yes, then list apartment
Thanks in advance for any help or suggestions.
I haven't cleaned it up yet, but this is working for me, so I wanted to share.
apartment_type.rb
class ApartmentType < ActiveRecord::Base
has_many :apartments, :dependent => :nullify
end
census_detail.rb
class CensusDetail < ActiveRecord::Base
belongs_to :resident_contacts
belongs_to :apartments
scope :get_current_residents, ->(id) { where("status = ? and apartment_id = ?", "current", id) }
end
apartment.rb where most of the work is happening
class Apartment < ActiveRecord::Base
belongs_to :apartment_type
has_many :census_details
has_many :residents, :through => :census_details
scope :rent_ready, -> { where(:enabled => true) }
def self.available
available_apartments = []
rent_ready_apartments = self.rent_ready.all
rent_ready_apartments.each do |apt|
tmp = ApartmentType.where('id = ?', apt.apartment_type_id).pluck(:occupancy)
occupancy = tmp[0]
current_residents = CensusDetail.get_current_residents(apt.id)
resident_count = current_residents.count
if resident_count < occupancy
available_apartments << apt
end
if resident_count = occupancy
scheduled = false
current_residents.each do |res|
if res.moveout_date
scheduled = true
end
end
if scheduled == true
available_apartments << apt
end
end
end
available_apartments
end
end
The code is a bit ugly but its so far passing my tests. I would have edited my original question instead of answering in case someone has a better way of doing this but each time I do my question gets voted down. If anyone knows a better way please let me know because I'm at a point in the app where there are about 50 tables and now it's all getting tied together with complex queries.

Rails: Method for joining if a user has liked a set of songs

I have the following models:
User
Playlist
SongLike
Song
...
When I query a playlist for all of its songs, I get an array of song objects returned. What's the most efficient way to find out which of these songs a user has "liked"? The likes are stored in the SongLike model:
class SongLike < ActiveRecord::Base
belongs_to :user
belongs_to :song, :counter_cache => "likes_count"
end
...this is the song model:
class Song < ActiveRecord::Base
has_and_belongs_to_many :playlists
has_many :featured_songs
has_many :song_likes
has_many :users, :through => :song_likes
...
end
Utilize your song_likes association along with the songs in memory rather than querying the db multiple times:
# user.rb
def songs_liked_from(subset)
like_ids = self.song_likes.where(:song_id => subset.map(&:id)).select(:song_id).map(&:song_id)
subset.select{|s| liked_ids.include?(s.id) }
end
You could put an index on user_id and song_id being looked up together and maybe make it unique if that works for your system.
def songs_user_liked
user_liked_songs = []
songs.each do |song|
song_like = SongLike.find_by_user_id_and_song_id(#,song)
if song_like.song_like_count > 0
user_liked_songs << song
end
end
user_liked_songs
end

Resources