Determine if children added/deleted in Rails Callback - ruby-on-rails-3

Scenario:
I have a habtm relationship and would like to determine if children in one direction have been added or deleted. I'm trying to use callbacks but find i don't have a record of changes to the children. Is there something like course.students.changed?
Using:
Rails 3.0.3
Ruby 1.9.2p0
Tables:
students - id, first_name, last_name
courses - id, name, location
courses_students - course_id, student_id
Models:
class Course
# Callbacks
before_save :student_maintenance
# Relationships
has_and_belongs_to_many :students
protected
def student_maintenance
# I want to do something like
students.changed?
students.changes.each do |student|
if marked_for_deletion
# do something
elsif marked_for_addition
# do something else
end
end
end
end
end
class Student
# Relationships
has_and_belongs_to_many :courses
end

If you want to capture when a student has been added or removed from a course, why not use a class for the join table *courses_students*? Since that's the place where an entry will be actually created or destroyed you could easily use after_create/after_destroy callbacks in there.

Similar to polarblau's answer -- but a tad more explanation, I hope:
In textual, UML terms, the relationship might be this:
Course ------*-> Student
Read: A Course has 0 to Many Students
You can add and remove students from the association at will.
However, you are interested in knowing more about the association, that is, when the student was added or dropped from the course. This "extra" information about an association leads to discovering you need another class, a.k.a. "Association Class."
So now you would insert that class in between your existing classes as so, taking the liberty to call it a "Registration":
Course ------*->Registration------1->Student
Read: A Course has 0 to Many Registration. Each Registration must have exactly 1 Student.
The Registration class might look like this:
+--------------+
| Registration |
+--------------|
| student |
| time_added |
| time_dropped |
+--------------+
So you can easily get a list of current students for a course (where time_dropped is nil). Or a list of dropped students (where time_dropped is not nil).

Related

Rails: Is this a use case for Single Table Inheritance (STI)?

Consider this setup. Please understand that our setup is much more detailed but this is a simple example.
competition which has name. This is an annual competition.
competition_instances which has location, starts_at.
Each competition has sports which has name.
Example:
competition.name: "Super Bowl" has different competition_instances every year but sport remains the same.
Conversely, competition.name: "Olympics" has different competition_instances and different sports in each competition_instance.
Would it be best to create competition_sports and competition_instance_sports with competition_instance_sports as a subclass of competition_sports?
GOAL: Use competition_instance_sports records if they exist, otherwise use the competition_sports record. In our real world app, each competition/competition_instance can have 20-50 sport records. How can we best achieve?
Based on what I understand from the question I cannot see where STI will be helpful in this situation. However a join table will get you where you want.
I suggest creating a new table sports, this model will have all the specific details of each sport. The competition_instance.rb will have one/many sport.rb. competiton.rb will have many sports through competition_instance.rb.
competition.rb
Class Competition < ActiveRecord::Base
has_many :competition_instances
has_many :sports, through: :competition_instances
end
competition_instance.rb
Class CompetitionInstance < ActiveRecord::Base
belongs_to :competition
belongs_to :sport
end
sport.rb
Class Sport < ActiveRecord::Base
has_many :competition_instances
end
By using this design you will be able to achieve the following:
1- You will have your predefined sports in your database along with their specific properties.
2- Each competition will have .sports which will give all the sports in this competition for the olympics case.
3- You will be able to set specific properties for each competition instance (example event_start_time and event_end_time) in the competition instance table.
I'm just thinking of the case where there are standard sports which are always in the Olympics and some which are added on, such as those proposed by the host country.
I would use Polymorphic Associations, in a "reverse manner".
class Competition < ActiveRecord::Base
has_many :competition_instances
has_many :competition_sports, as: :event
end
class CompetitionInstance < ActiveRecord::Base
belongs_to :competition
has_many :competition_sports, as: :event
def events_array # neater by sacrificing ActiveRecord methods
competition.competition_sports + competition_sports
end
def events # messier, returns ActiveRecord relationship
CompetitionSport.where( " ( event_id = ? AND event_type = 'Competition' ) OR
( event_id = ? AND event_type = 'CompetitionInstance')", competition_id, id )
end
end
class Sport < ActiveRecord::Base
has_many :events, as: :competition_sport
end
class CompetitionSport < ActiveRecord::Base
belongs_to :sport
belongs_to :event, polymorphic: true
end
This allows:
competition.competition_sports # standard sports
competition_instance.competition_sports # only those specific for this instance
competition_instance.events # includes sports from both
Aiming at the original question of "Is this a use case for STI", it will be difficult to say if this is or not without seeing the full complexity of your environment. But here's are some things to consider:
Abridged from How (and When) to Use Single Table Inheritance in Rails - eugenius blog:
STI should be considered when dealing with model classes that share much of the same functionality and data fields, but you want more granular control over extending or adding to each class individually. Rather than duplicate code or forego the individual functionalities, STI permits you to use keep your data in a single table while writing specialized functionality.
You've simplified your example, but it sounds like competition and competition_instance are NOT essentially the same object, with minor behavioral differences. With what you've described, I would probably rename these objects to event and competition, or whatever makes better sense to you in terms of illustrating what these objects are actually representing. I think of 'The Olympics' as a generic event, and '100m Dash' as a competition taking place at The Olympics. Or "The SuperBowl" only hosted one competition in 2014, "SuperBowl XLIX".
You've also tagged this question with database-normalization, which STI won't help. If the attributes differ slightly between your shared objects you'll end up with null fields everywhere.
I refer you to the other answers here to see how you should probably set those objects up to behave as desired.
This is not a case for single table inheritance, because competition_instance is not a substitute for competition and 1 competition can have many competition_instances. So you have 3 tables:
competitions
sports
competition_instances
competition_instances has a foreign key to competitions because 1 competition can have many competition_instances but each competition_instance has exactly one competition.
Whether you attach sports to competitions or competition_instances depends on the specific constraints of your use case. I don't exactly know what you mean by "each competition/competition_instance can have 20-50 sport records". I would expect each competition_instance to have exactly one sport, so you might leave it at that, or you might attach a collection of sports to a competition as well, so that you can retrieve new competitions by sport before there is a competition_instance. I'd need more details on your use case to give you further advice.

Rails 3 HasMany Through with STI

Here is my STI Models:
class User < ActiveRecord::Base
end
class Athlete < User
has_many :sports, :through => :user_sports
has_many :user_sports
end
class Coach < User
end
The UserSports table has user_id and sport_id... but then you run this:
athlete = Athlete.all.last
athlete.sports
The SQL that is generated is trying to use the athlete_id instead of the user_id... not too sure what I am doing wrong here... any suggestions would be greatful!
I'm not sure why you have a UserSports table. You can just use a foreign key for either User or Sport, depending on their relation to each other.
The User model would need a specified relation to the Sport model, and vice versa.
More information on that is here: http://guides.rubyonrails.org/association_basics.html#the-has_many-association
It makes sense that it's trying to pull the athlete_id instead of the user_id, since you are calling on an Athlete object.
As a side note: There is no need to write Athlete.all.last - you only need to write Athlete.last.

Best way to add a second parameter to route

I want my route to be something like cars(/:country/):car_id, what is the best way to do that? Only "cars" will list all the cars and "cars /: country" will list all the cars that are made in that country.
Now I have my route like this resources: cars,: path => "cars (/:country)" and I check in cars#index action if params[:country] is nil to determine what will be retrieved from the database .
My solution feels wrong and ugly and I guess the best solution and cleanest would be to make a country model, but do not really know how to organize it all up, tips?
country must have a slug and so do car_id too (using friendly_id for car_id). It feels like I should have a car table with name and slug thats all i have figured out.
Thanks!
First I'd say that your current solution is NOT ugly, nor wrong, at worst it's pedestrian. But without seeing all the involved models and associations, I can only give a general answer.
First, A country model, probably a good idea, but how do you relate it to the cars model?
You could do this:
class Country << ActiveRecord::Base
has_may :cars
end
class Car << ActiveRecord::Base
belongs_to :country
end
That would support semantics where by you could select a country, and get all cars belonging to a certain country, i.e.
#cars = Country.find('USA').cars
OR, you could do something like:
class Car << ActiveRecord::Base
has_one :country
end
class Country << ActiveRecord::Base
end
That would enable a different semantic:
#country = Car.find('Jeep').country
The point is to think of the query semantics you'd like to have in your app, and then define your associations to support the semantics that make sense for your app. I've posted very simple associations, you may end up with multiple and more complex associations, just depends on how you need to query the database and the associated models.
UPDATE
You posted:
I want my route to be something like cars(/:country/):car_id,
That doesn't make sense, if you know the specific car_id, you don't need any filtering or extra searching.
Sound like you want these URLs:
/cars # all cars
/cars/:country # all cars in country
/car/:id # a specific car
The first and third routes are probably there assuming you've defined the full set of RESTful routes for cars, i.e.
config/routes.rb
resources :cars
You just need to add to routes.rb:
GET '/cars/:country' => 'cars#index'
Then in app/controllers/cars_controller.rb:
def index
if params[:country]
#cars = Car.where("country_id = ?", params[:country])
else
#cars = Car.all
end
end
This assumes you have a relationship set up whereby each car record has a country_id attribute. That can come about in several ways, for example:
class Car < ActiveRecord::Base
belongs_to :country
end
That says my car table has a country_id attribute, and I can do something like:
#car = Car.find(1001)
"The car is in #{#car.country.name}"

ActiveRecord Query Count Number of Relations

This is a Ruby 1.9.3/Rails 3.2 project.
Let's say I have a model called Role, and a model called Employee, linked through a has_many/belongs_to relationship. A role has many employees, and an employee belongs to a role. Both of these models belong to a Store object that has many employees and roles.
Each role has a target_headcount attribute, representing the ideal number of employees in that position. From there, I have methods like the following for Role:
class Role < ActiveRecord::Base
# ...
# Number of employees currently filling this role.
def current_headcount
employees.count
end
# Number of headcount above or below the target.
def variance
current_headcount - target_headcount
end
end
Frequently, I need to get a collection of each role for which there is open headcount. I was doing this using the following class method for Role:
def self.open_headcount
all.select { |r| r.variance < 0 }
end
However, I'm now using meta_search, a RubyGem which requires an ActiveRecord::Relation object. I'd like to change open_headcount from a class method to a scope so that it returns an ActiveRecord::Relation object, but I'm not sure if it is possible.
This is super old, but FTR I probably could have done it with by using a SQL query to count the number of employees for the role and subtracting from it the value of the target column.

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.