I need a second set of eyes on this. When I create a Match between two players, Tournament.players returns an empty array.
Code
class Tournament < ActiveRecord::Base
has_many :player_matches
has_many :matches
has_many :players, :through => :player_matches
end
class PlayerMatch < ActiveRecord::Base
belongs_to :player
belongs_to :match
belongs_to :tournament
end
class Player < ActiveRecord::Base
has_many :player_matches
has_many :matches, :through => :player_matches
has_many :tournaments, :through => :player_matches
end
You need to have a double :through relation:
player_matches through matches and players through player_matches.
class Tournament < ActiveRecord::Base
has_many :matches
has_many :player_matches, :through => :matches
has_many :players, :through => :player_matches
end
class PlayerMatch < ActiveRecord::Base
belongs_to :player
belongs_to :match
end
class Player < ActiveRecord::Base
has_many :player_matches
has_many :matches, :through => :player_matches
has_many :tournaments, :through => :player_matches
end
class Match < ActiveRecord::Base
belongs_to :tournament
has_many :player_matches
has_many :players, :through => :player_matches
end
Related
I have a genre and movie models they are associated with has many through. when calling from browser i'm receiving error about categorization which is join model.
Error: undefined local variable or method `categorizations' for Class:0x00000007be0120>
This is the controller...
class GenreController < ApplicationController
def updateList
result=Net::HTTP.get(URI.parse('url'))
json = JSON.parse(result)
json['genres'].each do |data|
obj = Genre.new(
tmdb_id: data['id'],
name: data['name']
)
end
end
end
this is model
class Genre < ActiveRecord::Base
has_many :categorizations
has_many :movies, :through => categorizations
end
i know that there should be validations for this but right now i'm just populating genre table. There should be a create but i'm just trying to see the result.
EDIT:
Model for categorization
class Categorization < ActiveRecord::Base
belongs_to :movie
belongs_to :genre
end
Model for Movies
class Movie < ActiveRecord::Base
has_many :roles
has_many :actors, :through => :roles
has_many :watchlists
has_many :users, :through => :watchlists
has_many :categorizations
has_many :genres, :through => :categorizations
has_many :videos
end
has_many :movies, :through => categorizations
You need to have a colon before "categorizations":
has_many :movies, :through => :categorizations
I have these models
class EventGroups < ActiveRecord::Base
has_many :festival_venues
has_many :venues, :through => :festival_venues
end
class Venue < ActiveRecord::Base
has_many :festival_venues
has_many :event_groups, :through => :festival_venues
end
class FestivalVenue < ActiveRecord::Base
belongs_to :event_group
belongs_to :venue
end
Now I want to create a Venue via Eventgroups, and record in the FestivalVenue should be created as well.
When i delete Eventgroups related record in Venue and
FestivalVenue should be deleted as well.
How can i do this?
class EventGroup < ActiveRecord::Base
has_many :festival_venues, dependent: :destroy
has_many :venues, :through => :festival_venues, :dependent => :destroy
end
class Venue < ActiveRecord::Base
has_many :festival_venues
has_many :event_groups, :through => :festival_venues
end
class FestivalVenue < ActiveRecord::Base
belongs_to :event_group
belongs_to :venue
end
Now if you have event_group variable bound to EventGroup object, you create Venue (along with its FestivalVenue) with:
venue = Venue.create(your_attributes)
event_group.venues << venue
In your below code, Model class name must be singular. Change class name EventGroups to EventGroup. Now it will work like a charm.
class EventGroup < ActiveRecord::Base
has_many :festival_venues, dependent: :destroy
has_many :venues, :through => :festival_venues, :dependent => :destroy
end
Remaining code is good.
class Venue < ActiveRecord::Base
has_many :festival_venues
has_many :event_groups, :through => :festival_venues
end
class FestivalVenue < ActiveRecord::Base
belongs_to :event_group
belongs_to :venue
end
Hope it will help. Thanks
I have following models:
Product:
class Product < ActiveRecord::Base
has_many :product_recommendations, :dependent => :destroy
has_many :recommendations, :through => :product_recommendations
end
ProductRecommendation:
class ProductRecommendation < ActiveRecord::Base
belongs_to :recommendation
belongs_to :product
end
Recommendation:
class Recommendation < ActiveRecord::Base
has_many :product_recommendations, :dependent => :destroy
has_many :products, :through => :product_recommendations
has_many :recommendation_ratings, :dependent => :destroy
has_many :ratings, :through => :recommendation_ratings
end
Rating:
class Rating < ActiveRecord::Base
has_many :recommendation_ratings, :dependent => :destroy
has_many :recommendations, :through => :recommendation_ratings
end
RecommendationRating:
class RecommendationRating < ActiveRecord::Base
belongs_to :recommendation
belongs_to :rating
end
How would I be able to calculate the average rating for a given recommendation? My ratings table contains just 4 records, (Ratings 1-4), and I have an action in my recommendation controller which updates the RecommendationsRatings join table to associate a rating with a recommendation. Thanks!
have you tried:
class Product < ActiveRecord::Base
has_many :product_recommendations, :dependent => :destroy
has_many :recommendations, :through => :product_recommendations
has_many :ratings, :through => :recommendations
end
and then
p = Product.find 1
p.ratings.average(:rating)
this is assuming the Ratings model has an int / float called rating which stores the rating
I am trying to build a twitter like data model in rails. This is what I have come up with.
class User < ActiveRecord::Base
has_many :microposts, :dependent => :destroy
end
class Micropost < ActiveRecord::Base
belongs_to :user
has_many :mentions
has_many :hashtags
end
class Mention< ActiveRecord::Base
belongs_to :micropost
end
class Hashtag < ActiveRecord::Base
belongs_to :micropost
end
Should I be using a has_many through association somewhere or is this accurate?
Edit: The final twitter MVC model.
class User < ActiveRecord::Base
has_many :microposts, :dependent => :destroy
userID
end
class Micropost < ActiveRecord::Base
belongs_to :user
has_many :link2mentions, :dependent => :destroy
has_many :mentions, through: :link2mentions
has_many :link2hashtags, :dependent => :destroy
has_many :hashtags, through: :link2hashtags
UserID
micropostID
content
end
class Link2mention < ActiveRecord::Base
belongs_to :micropost
belongs_to :mention
linkID
micropostID
mentionID
end
class Mention < ActiveRecord::Base
has_many :link2mentions, :dependent => :destroy
has_many :microposts, through: :link2mentions
mentionID
userID
end
Edit 2: A concise and accurate explanation
http://railscasts.com/episodes/382-tagging?view=asciicast
If two microposts use the same hashtag, you probably don't want to create two database records for that hashtag. In this case you would use has_many through:
class Hashtagging < ActiveRecord::Base
belongs_to :micropost
belongs_to :hashtag
end
class Hashtag < ActiveRecord::Base
has_many :hashtaggings
has_many :microposts, through: :hashtaggings
end
class Micropost < ActiveRecord::Base
...
has_many :hashtaggings
has_many :hashtags, through: :hashtaggings
end
When you create the Hashtagging migration, make sure it has the micropost_id and hashtag_id columns.
My setup is as follows:
class User < ActiveRecord::Base
has_many :owners, :dependent => :destroy
has_many :properties, :through => :owners
end
class Owner < ActiveRecord::Base
belongs_to :user
belongs_to :property
end
class Property < ActiveRecord::Base
has_many :owners, :dependent => :destroy
has_many :users, :through => :owners
has_many :datafiles, :dependent => :destroy
end
class Datafile < ActiveRecord::Base
belongs_to :property
end
Now I'd like to be able to do #user.datafiles.
I tried has_many :datafiles, :through => :properties, :source => :datafiles but there appears to be a problem with a :through on something that's already went to a :through. So how would I go about to try and manage what I'm trying to do here?
Thank you in advance.
2 approaches;
1>
class User < AR
has_many :owners, :dependent => :destroy
has_many :properties, :through => :owners
has_many datafiles
end
class Datafile < AR
belongs_to :user
belongs_to :property
end
Your requirement of user.datafiles should be fulfilled with this.
If you want a nested has_many through, you'll need to use a plugin which is the 2nd approach.
2>
You can find it here.
The plugin works out of the box and does the job.
How about something like:
#user.rb
def datafiles
Property.find(:all, :joins => :owners, :conditions => ['owners.user_id = self.id'], :include => :datafile).collect(&:datafile)