Or-ing relationships in rails - ruby-on-rails

I have a number of many to many relationships between two tables with conditionals representing different roles.
class Course < ActiveRecord::Base
...
has_many :course_mentors, :conditions => { :type_str => "mentor" }, :class_name => CourseUser
has_many :mentors, :through => :course_mentors, :source => :user
has_many :course_enrollees, :conditions => { :type_str => "enrollee" }, :class_name => CourseUser
has_many :enrollees, :through => :course_enrollees, :source => :user
...
end
To retrieve the contents of one of the associations on its own I can simply do #course.enrollees, #course.mentors etc. However sometimes it would be convenient to be able to get both enrollees and mentors together.
I have many different associations which makes it impractical to create additional associations for each combination of single associations. One can always do
(#course.enrollees + #course.mentors).sort
however this results in two requests to the database and possibly duplicate entries.
I have also investigated the merge function on associations, however this just returns an empty relation.
What is the best rails like way of doing this?

You can simply add a general association to CourseUser inside your Course model:
has_many :course_users
And then put a scope inside CourseUser model:
scope :of_type, lambda {|types| where(:type_str => types)}
Then you can access it via:
my_course = Course.first
my_course.course_users.of_type([:mentor, :enrollee])

Related

Rails: possible to use fallback for duplicate polymorphic associations?

Is there a way to define a polymorphic association that pulls from another poly assoc, but also then reverts to a backup poly assoc if the first one is empty? Maybe like this?
class Reservation < ActiveRecord::Base
has_many :trip_itinerary_entries, :through => :trips, :source => :itinerary_entries
has_many :template_itinerary_entries, :through => :templates, :source => :itinerary_entries
has_many :entries, :from => :trip_itinerary_entries, :backup => :template_itinerary_entries
Thanks!
I would set up the two polymorphic associations (in my model and the required columns in my DB) and then create a method for getting both of the items (you don't want to override them method otherwise you would't be able to write new relationships).
def get_entries
if entries.any?
entries
else
template_itinerary_entries
end
end
This way you can still use entries when you need to add items.
reserve = Reservation.new
reserve.entries << Entry.create()
But you could use the get_entries method to get either of the items.
reserve.get_entries
=> [template_itinerary_entries] # if there are no entries.

Saving a rails has_many association using multiple keys

I have the following models:
class Product < ActiveRecord::Base
has_many :product_recommendation_sets, :dependent => :destroy
has_many :recommendation_sets, :through => :product_recommendation_sets
end
class RecommendationSet < ActiveRecord::Base
has_many :product_recommendation_sets, :dependent => :destroy
has_many :products, :through => :product_recommendation_sets
has_many :recommendations
end
class Recommendation < ActiveRecord::Base
belongs_to :recommendation_set
end
And am adding recommendations recommendations_set like so:
p = Product.find_by_wmt_id(product) || Product.create( ItemData.get_product_data(product) )
recommendation = find_by_rec_id(rec_id) || create( ItemData.get_product_data(rec_id) )
rec_set = RecommendationSet.find_or_create_by_rating_set_id_and_model_version_and_product_id(rating_set.id, model_version, p.id)
sec_set.update_attributes(
:rating_set_id => rating_set.id,
:product_id => p.id,
:model_version => model_version,
:notes => note
)
sec_set.recommendations << recommendation
sec_set.save
prs = ProductRecommendationSet.find_or_create_by_recommendation_set_id_and_rating_set_id_and_product_id(rec_set .id, rating_set.id, p.id,)
prs.update_attributes(
:recommendation_set_id => rec_set.id,
:rating_set_id => rating_set.id,
:product_id => p.id
)
This works as expected, however my problem is that I have multiple recommendation_sets which belong to multiple products, and each of the recommendation_sets may have the same recommendation. By saving each recommendation to a recommendation_set as I am currently doing, if two recommendation_sets have the same recommendation, only one of the sets will add that recommendation. Is there anyway of saving each recommendation to multiple recommendation_sets using a secondary id, such as save by recommendation_id_and_product_id, or would I need to change this releationship to a has_many :through?
Based on your clarification, I think you basically have a many-to-many relationship between RecommendationSet and Recommendation. Presently, you have a one-to-many.
There are a couple of options:
Use the has_and_belongs_to_many method in both models to describe the relationship;
Manually create a "join" model and then give both RecommendationSet and Recommendation a has_many to this join model (with two corresponding belongs_to lines in the join model pointing to the other two models);
A has_many ... :through style, like you mentioned
Note that the first two options require you to have a join table.
If you require additional information on the join table/model, I tend to go with the 2nd option. Otherwise, either the first or third are perfectly valid.
Ryan Bates of RailsCasts made an episode about this here: http://railscasts.com/episodes/47-two-many-to-many
And some more information from the Rails documentation: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Many-to-many
In short, if you don't need extra info on the join, I think your idea of the has_many ... :through is perfectly fine.
Let me know whether that helps

Putting everything in the relationships model or adding a new model

I followed the Michael Hartle book Rails Tutorial and made a user following system that works through a relationships table, with a follower_id and a followed_id.
I want to add another relationship, this time a favoriting system. Would I be best to add the column to the relationships table and use that or should I create a new model to hold the favoriting relationship?
I don't think there is a definite answer to your question.
But to keep things simple I would consider to use only one Connection table with flags
is_followed
is_favorite
Especially if you can only favorite followed people, validation becomes a lot easier. Still allows easy accessors in your model
class Person < ActiveRecord::Base
...
has_many :favorites, :through => :connections, :conditions => { :is_favorite => true }, :source => ...
has_many :followers, :through => :connections, :conditions => { :is_followed => true }, :source => ...
has_many :followee, :through => :connections, :conditions => { :is_followed => true }, :source => ...
and by the way the foreign key relation you can declare is the u have to write t.reference:user in the favourite migration file if u want user foreign key as column in the favourite table

ActiveRecord siblings in many-to-many relationship

I have this working to a degree, but I am looking for some input on how to query for the siblings in a one to many relationship to see if there is a more elegant way of accomplishing this.
Consider the following classes
class Post < ActiveRecord::Base
has_many :post_categories
has_many :categories, :through => :post_categories
end
class Category < ActiveRecord::Base
has_many :post_categories
has_many :posts, :through => :post_categories
end
A post by definition can have multiple categories, what I would need this for is to show a "related posts" area on the site. Like I mentioned previously I do have a working version which is to simply do the following:
Post.find(id, :include => {:categories => :posts})
Looking at the logs the application then has to do five queries to get the end data that I am looking for.
Any thoughts are appreciated!
The only problem I see you having with what you have already is that you probably don't want to return all posts which share a category with a post.
#post = Post.find params[:id]
#related_posts = Posts.find(:all, :joins => :post_categories,
:select => "posts.*, count(post_categories) post_category_count",
:conditions => {:post_categories => {:category => #post.categories}},
:group => "posts.id", :order => "post_category_count desc")
This will return the most relevant posts first, ie. those which have the most shared categories, and you can either add a limit or paginate in order to limit results returned.
If you need support for large object trees, you might want to look at awesome nested set thought it may be overkill for this problem.

Rails polymorphic many to many association

I'm trying setup a generic sort of web of related objects. Let say I have 4 models.
Book
Movie
Tag
Category
I would like to able to do:
book = Book.find(1)
book.relations << Tag.find(2)
book.relations << Category.find(3)
book.relations #=> [Tag#2, Category#3]
movie = Movie.find(4)
movie.relations << book
movie.relations << Tag.find(5)
movie.relations #=> [Book#1, Tag#5]
Basically I want to be able to take any 2 objects of any model class (or model class that I allow) and declare that they are related.
Obviously I don't want to create a huge mess of join tables. This seems like it's not quite a has many through association, and not quite a polymorphic association.
Is this something that Rails can support via it's association declarations or should I be rolling my own logic here?
Support for polymorphism has improved dramatically since the early days. You should be able to achieve this in Rails 2.3 by using a single join table for all your models -- a Relation model.
class Relation
belongs_to :owner, :polymorphic => true
belongs_to :child_item, :polymorphic => true
end
class Book
has_many :pwned_relations, :as => :owner, :class_name => 'Relation'
has_many :pwning_relations, :as => :child_item, :class_name => 'Relation'
# and so on for each type of relation
has_many :pwned_movies, :through => :pwned_relations,
:source => :child_item, :source_type => 'Movie'
has_many :pwning_movies, :through => :pwning_relations,
:source => :owner, :source_type => 'Movie'
end
A drawback of this kind of data structure is that you are forced to create two different roles for what may be an equal pairing. If I want to see all the related movies for my Book, I have to add the sets together:
( pwned_movies + pwning_movies ).uniq
A common example of this problem is the "friend" relationship in social networking apps.
One solution used by Insoshi, among others, is to register an after_create callback on the join model ( Relation, in this case ), which creates the inverse relationship. An after_destroy callback would be similarly necessary, but in this way at the cost of some additional DB storage you can be confident that you will get all your related movies in a single DB query.
class Relation
after_create do
unless Relation.first :conditions =>
[ 'owner_id = ? and owner_type = ? and child_item_id = ? and child_item_type = ?', child_item_id, child_item_type, owner_id, owner_type ]
Relation.create :owner => child_item, :child_item => owner
end
end
end
I have come up with a bit of solution. I'm not sure it's the best however. It seems you cannot have a polymorphic has_many through.
So, I fake it a bit. But it means giving up the association proxy magic that I love so much, and that makes me sad. In a basic state, here is how it works.
book = Book.find(1)
book.add_related(Tag.find(2))
book.add_related(Category.find(3))
book.related #=> [Tag#2, Category#3]
book.related(:tags) #=> [Tag#2]
I wrapped it up in a reusable module, that can be added to any model class with a single has_relations class method.
http://gist.github.com/123966
I really hope I don;t have to completely re-implement the association proxy to work with this though.
I think the only way to do it exactly as you described is the join tables. It's not so bad though, just 6, and you can pretty much set-and-forget them.
depending on how closesly related your movies/books db tables are
what if you declared
class Items < ActiveRecord::Base
has_many :tags
has_many :categories
has_and_belongs_to_many :related_items,
:class => "Items",
:join_table => :related_items,
:foreign_key => "item_id",
:associated_foreign_key => "related_item_id"
end
class Books < Items
class Movies < Items
make sure you put type in your items table

Resources