Combining Polymorphic Associations with Rich Many-to-Many Associations - ruby-on-rails

Right now I have a rich many-to-many association with VideoVote as the independent record.
class VideoVote < ActiveRecord::Base
belongs_to :user
belongs_to :video
end
class User < ActiveRecord::Base
has_many :video_votes
has_many :voted_videos,
:through => :video_votes,
:source => :video
end
class Video < ActiveRecord::Base
has_many :video_votes
has_many :voted_users,
:through => :video_votes,
:source => :user
end
However, I want to trasform this into a polymorphic association where comments can also have many VideoVotes (I realize this is confusing, so I should probably change it to Votes). (also, a video will have many comments.) How should I do this?

You first want to add voteable_id:integer and voteable_type:string to your video_votes table.
Then your models will look like:
class VideoVote < ActiveRecord::Base
belongs_to :voteable, :polymorphic => true
end
class Comment < ActiveRecord::Base
has_many :video_votes, :as => :voteable
#code
end
class Video < ActiveRecord::Base
has_many :video_votes, :as => :voteable
#code
end
Then you can access them just like any other has_many:
#video.video_votes
#comment.video_votes
#etc.

Related

Best way to associate these models?

I have four models, Movies, Person, Cast, and Crew and I'm uncertain on what is best way to associate them all together. Should I use a has many through association or stick with the associations below? Any recommendations would be greatly appreciated.
I want to be able to visit a movie object's show page then be able to list the appropriate cast and crew associated with the movie. In addition, when I visit a Person's show page, I want to list of all their cast roles movies, in addition if they were part of the crew.
class Movie < ActiveRecord::Base
has_many :cast
has_many :crew
end
class Person < ActiveRecord::Base
has_many :cast
has_many :crew
end
class Cast < ActiveRecord::Base
belongs_to :movie
belongs_to :person
end
class Crew < ActiveRecord::Base
belongs_to :movie
belongs_to :person
end
class Movie < ActiveRecord::Base
has_many :cast_memberships
has_many :crew_memberships
has_many :cast_members, :through => :cast_memberships, :source => :person
has_many :crew_members, :through => :crew_memberships, :source => :person
alias_method :cast, :cast_members
alias_method :crew, :crew_members
end
class CastMembership < ActiveRecord::Base
belongs_to :movie
belongs_to :person
end
class CrewMembership < ActiveRecord::Base
belongs_to :movie
belongs_to :person
end
class Person < ActiveRecord::Base
end
class CastMember < Person
has_many :cast_memberships, :foreign_key => :person_id
has_many :roles, :through => :cast_memberships, :source => :movie
end
class CrewMember < Person
has_many :crew_memberships, :foreign_key => :person_id
has_many :jobs, :through => :crew_memberships, :source => :movie
end
> movie = Movie.create! name:"Star Wars"
> cast_member = CastMember.create! name:"Harrison Ford"
> movie.cast << cast_member
> movie.cast # [{name: "Harrison Ford"}]
> cast_member.roles # [{name: "Star Wars"}]
This isn't quite what you need--cast_member.roles should return the characters (["Han Solo"]) not the movies. You could add attributes to the cast_memberships and crew_memberships tables for character data or job descriptions.

HABTM relationships with polymorphism

I've got a Person who can be linked to many Structures (structure is polymorphic)
I've got a Venue, who can have many People, as a structure.
I've got a Journal, who can have many People, as a structure.
Here is my modelization :
class Venue < ActiveRecord::Base
has_many :structure_people, :as => :structure
has_many :people, :through => :structure_people
end
class Journal < ActiveRecord::Base
has_many :structure_people, :as => :structure
has_many :people, :through => :structure_people
end
class Person < ActiveRecord::Base
has_many :structure_people
has_many :structures, :through => :structure_people
end
class StructurePerson < ActiveRecord::Base
belongs_to :structure, polymorphic: true
belongs_to :person
end
My problem :
when i try to get people on a Venue or on a Journal, it works. Cool :)
BUT
when i try to get structures on a person, i've got an error :
ActiveRecord::HasManyThroughAssociationPolymorphicSourceError: Cannot have a has_many :through association 'Person#structures' on the polymorphic object 'Structure#structure'.
Anyone could help me to solve this ?
Thanks a lot.
Christophe
I think it's a restriction of Rails, because has_many association will guess a class_name automatically. But polymorphic association may returns multiple class_name. Do you mind use this:
class Person < ActiveRecord::Base
has_many :structure_people
has_many :venues, :through => :structure_people
#Journal is the same.
end
class StructurePerson < ActiveRecord::Base
belongs_to :structure, polymorphic: true
belongs_to :venue, :foreign_key => 'structure_id', :conditions => {:structure_type => 'Venue'}
belongs_to :person
end
Although it is an ugly solution...
I think you can choose an alternative way.
class Person < ActiveRecord::Base
has_many :structure_people
def structures
structure_people.map(&:structure)
end
end
You can't get chaining function of has_many, but you can get polymorphic structures :)

How do you model "Likes" in rails?

I have 3 models: User, Object, Likes
Currently, I have the model: a user has many Objects. How do I go about modeling:
1) A user can like many objects
2) an Object can have many likes (from different users)
So I want to be able to do something like this:
User.likes = list of objects liked by a user
Objects.liked_by = list of Users liked by object
The model below is definitely wrong...
class User < ActiveRecord::Base
has_many :objects
has_many :objects, :through => :likes
end
class Likes < ActiveRecord::Base
belongs_to :user
belongs_to :object
end
class Objects < ActiveRecord::Base
belongs_to :users
has_many :users, :through => :likes
end
To elaborate further on my comment to Brandon Tilley's answer, I would suggest the following:
class User < ActiveRecord::Base
# your original association
has_many :things
# the like associations
has_many :likes
has_many :liked_things, :through => :likes, :source => :thing
end
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :thing
end
class Thing < ActiveRecord::Base
# your original association
belongs_to :user
# the like associations
has_many :likes
has_many :liking_users, :through => :likes, :source => :user
end
You are close; to use a :through, relation, you first must set up the relationship you're going through:
class User < ActiveRecord::Base
has_many :likes
has_many :objects, :through => :likes
end
class Likes < ActiveRecord::Base
belongs_to :user
belongs_to :object
end
class Objects < ActiveRecord::Base
has_many :likes
has_many :users, :through => :likes
end
Note that Objects should has_many :likes, so that the foreign key is in the right place. (Also, you should probably use the singular form Like and Object for your models.)
Here is a simple method to achieve this. Basically, you can create as many relationships as needed as long as you specify the proper class name using the :class_name option. However, it is not always a good idea, so make sure only one is used during any given request, to avoid additional queries.
class User < ActiveRecord::Base
has_many :likes, :include => :obj
has_many :objs
has_many :liked, :through => :likes, :class_name => 'Obj'
end
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :obj
end
class Obj < ActiveRecord::Base
belongs_to :user
has_many :likes, :include => :user
has_many :users, :through => :likes
# having both belongs to and has many for users may be confusing
# so it's better to use a different name
has_many :liked_by, :through => :likes, :class_name => 'User'
end
u = User.find(1)
u.objs # all objects created by u
u.liked # all objects liked by u
u.likes # all likes
u.likes.collect(&:obj) # all objects liked by u
o = Obj.find(1)
o.user # creator
o.users # users who liked o
o.liked_by # users who liked o. same as o.users
o.likes # all likes for o
o.likes.collect(&:user)
Models & associations as per naming conventions of rails modeling
class User < ActiveRecord::Base
has_many :likes
has_many :objects, :through => :likes
end
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :object
end
class Object < ActiveRecord::Base
belongs_to :user
has_many :likes
has_many :users, :through => :likes
end
Also, you can use of already built-in gems like acts-as-taggable-on to have same functionality without code :)

Polymorphic has_many: through in rails

I have a tags model that I'd like to be polymorphic, but I don't want five records for a tag of "video" for example, I want to create the tag once and be able to use it on a variety of models. I've ready some of the questions here about doing that, but I'm not quite getting how to make it work.
So I've got:
class Tag < ActiveRecord::Base
belongs_to :tagable, :polymorphic => true
end
and
class Post < ActiveRecord::Base
has_many :tags, :through => :tag_assignments
end
and
class TagAssignment < ActiveRecord::Base
has_many :tags, :as => :taggable
end
Seems to me that should work, but... reading all the questions here I know I need a :source => option in there somewhere to tie it all together, but I'm just not following exactly how to do it. Can anyone help?
You have to redo your models as follows:
class Tag < ActiveRecord::Base
has_many :tag_assignments
end
class TagAssignment < ActiveRecord::Base
belongs_to :tagable, :polymorphic => true
belongs_to :tag
end
class Post < ActiveRecord::Base
has_many :tag_assignments, :as => :tagable
has_many :tags, :through => :tag_assignments
end
Now given a post you can get its tags as follows:
post.tags
Note
You should consider using the acts-as-taggable-on gem for your use case.

Simple Table Relation

Consider this:
class User < ActiveRecord::Base
# name, email, password
end
class Article < ActiveRecord::Base
# user_id, title, text
end
class Favorites < ActiveRecord::Base
# user_id, article_id
end
How do I put this all together in a way that I can have #user.articles (articles created by the user) and #user.favorite_articles (favorite articles from Favorite model)
Thanks in advance!
You can use a has_many :through association to fetch the favorite articles from user. You can also use it to fetch the users who favorited a given article.
class User < ActiveRecord::Base
has_many :articles
has_many :favorites
has_many :favorite_articles, :through => :favorites, :source => :article
end
class Article < ActiveRecord::Base
belongs_to :user
has_many :favorites
has_many :favorited_by_users, :through => :favorites, :source => :user
end
class Favorite < ActiveRecord::Base
belongs_to :article
belongs_to :user
end
If I understood right what you wanted, I'd say
class User < ActiveRecord::Base
has_many :articles
has_many :favorite_articles, :through => :favorites, :source => :article
end
class Article < ActiveRecord::Base
has_and_belongs_to_many :user
end
class Favorites < ActiveRecord::Base
has_and_belongs_to_many :user
has_one :article
end
edited: added favorite_articles

Resources