rails model relationship - ruby-on-rails

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 !

Related

Has many through Association - 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

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

has_many :through relationships explained

I'm new to Rails and have some doubts about the kind of relationship do I need to use. Here is the case.
I have two models Offer and User, a user could belong to to many offers and offers can have many user. Also the users create the offers.
I think I have to use a has_many :through ralationship. For example I've created another model "Applicant". Applicant belongs_to user and belongs_to offer. But how is the relationship from the user and offer model? For example:
User Model
has_many :offer, :through => :applicant
Offer Model
has_many :user, :through => :applicant
My doubt is because I already have this two relationship
User Model
has_many :offers, :dependent => :destroy
Offer Model
belongs_to :user
After solve this, I guest I have to save the record in the applicant model from the applicanst_controller, right?
Thanks in advance
What you have described is a many-to-many relationship using a join table. You're actually pretty close but you just need to remove the has_many :offers, :dependent => :destroy from your user model and the blongs_to :user in your offer model. It should look something like this:
class User < ActiveRecord::Base
has_many :offers, :through => :applicants
end
class Applicant < ActiveRecord::Base
belongs_to :users
belongs_to :offers
end
class Offer < ActiveRecord::Base
has_many :users, :through => :applicants
end
You don't have to worry about the dependent destroy part as associations are automatically removed as the corresponding objects are removed. With a many to many association it doesn't really matter how you go about building the relationship. Either of the following will work:
#user.offers << #offer
#offers.users << #user
If you don't need to store any information specific to your applicant join table (e.g., time stamps, descriptions) you might instead want to look at a has_and_belongs_to_many relationship. Check out choosing between has_many_through and has_and_belongs_to_many for reference.
Edit
Heres the code for a HABTM relationship:
class User < ActiveRecord::Base
has_and_belongs_to_many :offers
end
class Offer < ActiveRecord::Base
has_and_belongs_to_many :users
end

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

Multiple relationships to the same models

I'm having trouble figuring out the best way to do this. I have a User model and a Tournament model and I set up a has_many :through relation between these two models called 'followed_tournaments' so that users can follow a tournament. As such, I already have a has_many :tournaments in the User model and a has_many :users in the Tournament model so that a tournament has many followers and a user can follow many tournaments.
I'd like to set up another habtm or has_many :through relationship so that a User can be considered a "contributor" to a Tournament -- a completely separate relationship than what I already set up. I'd like a tournament to have any number of contributors and the user to contribute to many tournaments.
What's the best way to go about implementing this?
Use source or class_name
class Tournament < ActiveRecord::Base
has_many :users # ... whatever
has_many :contributions
# using class_name
has_many :contributors, :through => :contributions
# using source
has_many :contributors, :through => :contributions, :source => :user
end
class Contribution < ActiveRecord::Base
belongs_to :tournament
# using class_name
belongs_to :contributor, :class_name => 'User'
# using source
belongs_to :user
end

Resources