Rails: Concat Two CollectionProxies with two different models - ruby-on-rails

As the title explains, say I have two ActiveRecord::Base models: SatStudentAnswer and ActStudentAnswer.
I have a Student model
has_many :act_student_answers
has_many :sat_student_answers
I would like to create a collection proxy that concats them together so I can query all student answers together.
student.sat_student_answers.concat(student.act_student_answers)
However I get an
ActiveRecord::AssociationTypeMismatch: SatStudentAnswer(#70111600189260) expected, got ActStudentAnswer(#70111589566060) error.
Is there a way to create a collection proxy with two different models so I can continue using the Active Record query interface? If not, what would be the best way to solve this problem?
Thank you for the help!

As far as I know, there isn't way to create Rails AR:Relation with different models in it (I digged in this issue when I needed to create newsfeed with different kind of posts). My solution wasn't very beautiful, but was working: I've created additional model with polymorphic has_many. You could query collection of these new defined models and distinguish sat_answers and act_answers by field answer_type.

You can do it like this
array = student.sat_student_answers + student.act_student_answers
Then for example
array.each {|item| p item.student}

It seem like you actually want the two models to be just one model categorized into two things act and sat.
class Student < ActiveRecord::Base
# ...
has_many :answers
# ...
end
class Answer < ActiveRecord::Base
# ...
scope :act, -> { where kind: :act }
scope :sat, -> { where kind: :sat }
# ...
end
Then you'll be able to do your concat.
student.answers.act.concat student.answers.sat
# that is similar to
student.answers.act << student.answers.sat
However, concat methods adds the records to the receiving association and saves it in the DB if the owner of the association is persisted. I'm not really sure what you're trying to do there. Are you mixing the ACT answers with SAT answers making SAT answers include the ACT answers?
See: http://apidock.com/rails/ActiveRecord/Associations/CollectionAssociation/concat
What you probably want is below:
student.answers
# or
act_and_sat_answers = student.answers.where(kind: [:act, :sat])
# chain more filter query
act_and_sat_answers.where(correct: true)
If you really want to use two distinct models then you'll have to create your own collection proxy.

Related

Rails has_and_belongs_to_many query for all records

Given the following 2 models
class PropertyApplication
has_and_belongs_to_many :applicant_profiles
end
class ApplicantProfile
has_and_belongs_to_many :property_applications
end
I have a query that lists all property_applications and gets the collection of applicant_profiles for each property_application.
The query is as follows and it is very inefficient.
applications = PropertyApplication.includes(:applicant_profile).all.select |property_application| do
property_application.applicant_profile_ids.include?(#current_users_applicant_profile_id)
do
assume #current_users_applicant_profile_id is already defined.
How can I perform one (or few) queries to achieve this?
I want to achieve something like this
PropertyApplication.includes(:applicant_profile).where('property_application.applicant_profiles IN (#current_users_applicant_profile))

Rails have ActiveRecord grab more than one association in one go?

The question below had a good answer to grab associated values of an activerecord collection in one hit using Comment.includes(:user). What about when you have multiple associations that you want to grab in one go?
Rails have activerecord grab all needed associations in one go?
Is the best way to just chain these together like below Customer.includes(:user).includes(:sales).includes(:prices) or is there a cleaner way.
Furthermore, when I am doing this on a loop on an index table. Can I add a method on the customer.rb model so that I can call #customers.table_includes etc and have
def table_includes
self.includes(:user).includes(:sales).includes(:prices)
end
For the record I tested the above and it didn't work because its a method on a collection (yet to figure out how to do this).
In answering this, I'm assuming that user, sales, and prices are all associations off of Customer.
Instead of chaining, you can do something like this:
Customer.includes(:user, :sales, :prices)
In terms of creating an abstraction for this, you do have a couple options.
First, you could create a scope:
class Customer < ActiveRecord::Base
scope :table_includes, -> { includes(:user, :sales, :prices) }
end
Or if you want for it to be a method, you should consider making it a class-level method instead of an instance-level one:
def self.table_includes
self.includes(:user, :sales, :prices)
end
I would consider the purpose of creating this abstraction though. A very generic name like table_includes will likely not be very friendly over the long term.

Selecting only associations between engines

I need to grab all users that have an application. User is part of my core engine, which is used by many other engines. I'd like to keep User unaware of what is using it, which is why I don't want to add has_many :training_applications in my User model.
Here are the classes
module Account
class User < ActiveRecord::Base
end
end
module Training
class TrainingApplication < ActiveRecord::Base
belongs_to :user, class: Account::User
end
end
The following obviously won't work because User has no concept of TrainingApplication:
Account::User.joins(:training_application).distinct
Is there an elegant way to return a distinct collection of User objects that are associated with a TrainingApplication?
What I landed on as a quick solution is
Account::User.where(id: Training::TrainingApplication.all.pluck(:user_id))
but I'm thinking that there's a better solution.
In case there is no way you can add a has_many :training_applications association to the User, the following should be suitable solutions:
You could type up a joins string yourself:
t1 = Account::User.table_name
t2 = Training::TrainingApplication.table_name
Account::User.
joins("INNER JOINS #{t2} ON #{t2}.user_id = #{t1}.id").
group("#{t1}.id")
For the sake of variety, let me cover the subquery method as well:
Account::User.where("id IN (SELECT user_id FROM #{t2})")
I would go with the joins method but I believe both solutions will be faster than your current implementation.

In Rails, what do keywords belongs_to, has_many, etc, actually do?

I understand the concept of relational databases, primary/foreign keys, etc, however, I'm having trouble seeing the actual result of setting these properties up in my models. Do they generate helper functions or something similar? or do they simply work on a database level?
For example, if I have these two models (other properties omitted)
class Course < ActiveRecord::Base
has_and_belongs_to_many :schedules
has_many :sections
end
class Section < ActiveRecord::Base
belongs_to :course
end
I could simply get all sections for any given course like this:
Section.where(course_id: 1234)
However, I could do this without having set up the relations at all.
So my question is: Why do we do this?
Adding these methods let's you do things like this:
Section.find(5).course # <== returns a 'Course' model instance
Also it let's you join your queries more easily:
Section.joins(:course).where(course: {name: "English"}) # <== returns sections who have the 'english' course
Neither of these would be possible if you didn't set up the relations in the model.
Similarly:
Course.find(8).sections # returns an array of sections with 'course_id = 8'
It makes your calls more semantic, and makes things easier from a programmatic perspective :)
Relations are applied on instances of an object. So, these relations allow you to get related objects to an instance of another.
For example, say you have an instance of Section (called #section). You'd be able to get all Course objects for that section by doing:
#section.course if you have belongs_to :course set up.
Similarly, if you have an instance of Course, you can get all Section objects for that Course with:
#course.sections if you have has_many :sections.
TL;DR - these are helper scopes for instance variables of Course and Section.

how to enforce inclusion rules in ActiveRecord associations

I'm new to rails and I have a question on how best to enforce custom rules on my model associations.
For example, suppose I have:
class Person < ActiveRecord::Base
belongs_to :organization
end
class Organization < ActiveRecord::Base
has_many :people
end
and now suppose that I only want to allow the Organization.people << Person.new(...) command to succeed if the new Person object is compatible with the other people that were previously added to the Organization. This would entail running a validation check across all the existing elements of Organization.people and deciding whether the new Person could be added or not.
It seems to me that I can do this by overriding all the Organization.people assignment operators (such as << and =) and putting my validation logic in the override routine.
Is this best way to accomplish this?
Thanks!
I think you could put a validation in the Person class. It would run a test against the other people in self.organiation.people. I don't know if I would override the << on the has many relationship only because if you decide to create a person like Person.new(:organization => some_org) your << override would not get used. If the validation lives on the Person class, it would get exercises no matter how you create the person.

Resources