Has many through Association - Rails - ruby-on-rails

What should be the association for the following criteria.
A book can have many ratings
User can add many ratings
User can add only one rating for a single book
Tables
books - id, title, description
ratings - id, book_id, user_id, value
When a book is selected by the logged in user, i need to get the book details and the rating given by the logged in user.
Something like
b = Book.includes(:ratings)
Should i use has many through association here?
Any help would be appreciated.

I have figured it out.
user.rb
has_many :books
has_many :ratings, through: :books
book.rb
has_many :ratings
has_many :users, through: :rating
rating.rb
belongs_to :book
belongs_to :user

Related

How to reference a field in the join model of a has_many though Rails 5 relationship

I am working on an app where users have many quizzes and quizzes can have many users. I have set the relationships:
class User < ApplicationRecord
has_many :studies
has_many :quizzes, through: :studies
end
class Quiz < ApplicationRecord
has_many :studies
has_many :users, through: :studies
end
class Study < ApplicationRecord
belongs_to :user
belongs_to :quiz
end
I have a field in the Study table to store the score that the user made on the quiz, but I am unable to access the field. I have tried #quiz.studies.score and #quiz.study.score but Rails give me an undefined method. How to I access the field in a join model of a has_many though relationship?
#quiz.studies return the collection of studies. So you have to use first, last, each to get the score of the specific studies.
Try this:
#quiz.studies.first.score

Association for model - has_many :through

I have three models - Company, User and CompanyUser. The associations are as follows.
Company.rb
has_many :company_users
has_many :users, :through => :company_users
User.rb
has_many :company_users, :dependent => :destroy
belongs_to :company
CompanyUser.rb
belongs_to :company
belongs_to :user
For fetching current_user.company, what moddifications are to be made in the model association?
Any help would be appreciated.
It should be:
has_many :companies, through: :company_users
A has_many :through association is often used to set up a many-to-many
connection with another model. This association indicates that the
declaring model can be matched with zero or more instances of another
model by proceeding through a third model.
So if you are creating three models and making a has_many :through association I believe that User will have many Companies and Company will have many Users.
But if you need that the user belongs to only one company instead of creating the third model save the company_id in the users table itself.
Update:
Now as your scenario is A company can have may users and User belongs to a single company, you need two models: User and Company. Your User model should have an attribute company_id and then company_id should be saved in users table only. Then the associations as follows:
class User < ActiveRecord::Base
belongs_to :company
end
class Company < ActiveRecord::Base
has_many :users
end
Then you can do current_user.company
You can get more information on associations in the RailsGuides
According to the associations you have taken,
user already have as association with the company through the Company User model, so user may have many companies according to your associations.
so,
class User < ActiveRecord::Base
has_many :company_users, :dependent => :destroy
has_many :companies, :through => :company_users
end
current_user.companies will give you the companies.
But if you need only one company for a user then,
class User < ActiveRecord::Base
belongs_to :company
end
take belongs_to company and save company_id in users table,
then you can call,
`current_user.company`
According to your logic,
I think you may need to create a new variable session current_company_user which is object CompanyUser.
And then, to fetch company by :
current_company_user.company

Model User has many posts and can create post

Hello i have trouble with logic. In my app users can create Posts and add them to favourites. The problem is in assiciations on Posts and Users. When User creates Post user_id is applied to posts table. How can i make associations when other user or this one add Post to favourite.
You need to create another table that will join a post and user. You can call that table favorites with 2 columns: post_id and user_id
class Favorite < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
class User < ActiveRecord::Base
has_many :posts
has_many :favorites
has_many :favorite_posts, through: :favorites, source: :post
end
class Post < ActiveRecord::Base
belongs_to :user
has_many :favorites
has_many :favorited_by_users, through: :favorites, source: :user
end
You could create an new model/table for association. I would take a many to many relation for this.
Table: Bookmark
user_id | post_id
How a has many :through relationship in rails work is discriped here:
http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association

Ruby: ActiveRecord relationship

I have some problems setting up my desired relationship in my application. Some help and hints would be appreciated!
I have the following models:
User (id, username)
Company (id, name)
Campaign (id, name, company_id)
Relationship (user_id, company_id)
The relationship is supposed to connect the user to many companies.
Company has_many campaigns.
I want to to connect all the campaigns related to the companies that the specific user follows.
Users > (Relationships) > Companies > Campaigns
I'd better not post some code since it's just a mess and not at all doing what I want.
I've also really tried to follow railstutorial.org, http://ruby.railstutorial.org/chapters/following-users#top and change it the way I want with no success.
I need your help. :)
Should be pretty straightforward! This is obviously pseudocode, but here you go:
User
has_many :relationships
has_many :companies, :through => :relationships
has_many :campaigns, :through => :companies
Relationship
belongs_to :user
belongs_to :company
Company
has_many :relationships
has_many :users, :through => :relationships
has_many :campaigns
Campaign
belongs_to :company

rails model relationship

Hi I am really new to rails and I am trying to create a products ratings model.
So there's users (name, email, pw)
Users has a list of products that the user has rated. With a rating (1-10) and a comment.
Each product has its description, a list of the users who rated them, the rating and the comment.
How should I create the relationship? Should I have 3 models, user, rating, product, or can I get by with just user and product?
Also what would be the :has_many .etc relationship look like?
Here's what I would do
class User
has_many :ratings
has_many :products, :through => :ratings
end
class Product
has_many :ratings
has_many :users, :through => :ratings
end
class Rating
belongs_to :user
belongs_to :product
end
This way if you wanted to get all the users that have rated a product, you can say product.users.
This would a great case for a has_many :through =>
User Model.
User has_many :ratings
User has_many :products, :though => :ratings
Rating Model.
belongs_to :user
belongs_to :product
Product Model.
Product has_many :ratings
Product has_many :users, :through => ratings
n.b. this is now considered superior to has_and_belongs_to_many which many folks consider to be basically deprecated at this point.
Personally I've never liked using has_many_and_belongs_to, both as it works and also because of the frequent re-work to turn it into has_many, :through as soon as an additional attribute is desired on the join model (ratings in this case).
Actually you want a rating 'level' so you already have a case for the has_many, :through !

Resources