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
Related
Could you please help me out with this query? I still can't figure out how to achieve that.
There is a team model that has users and lists.
team
has_many :lists
has_many :users
And there is also a many to many relationship between users and lists that represents which lists the user has access to.
list
has_many :accesses
has_many :users, through: :accesses
belongs_to :team
user
has_many :accesses
has_many :lists, through: :accesses
belongs_to :team
access
belongs_to :list
belongs_to :user
I would like to get all the lists that belong to the users' team, but which user don't have access too.
I tried this, but that doesn't seem right:
List.joins(:team).joins(:user).where.not(users: { id: #user.id })
I also have a method called team.lists that allows to grab all lists of a team, but joins doesn't work on it.
What about splitting this a bit?
users_lists_ids = #user.lists.pluck(:id)
#user.team.lists.where('id NOT IN (?)', users_lists_ids)
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
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
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 !
I've got a Rails 3.1 app with a User model and a House model (this is like a group). I've set up a many-to-many relation with join model Membership between those two and there are methods to manages roles of a user in some house in the join model.
But my problem is that a User has only one house and not many. So I always have to do user.houses.first to get his house (I've set up a helper house which does that) but the design is not good so I've tried to put has_one :membership and has_one :house, :through => :membership instead of has_many :memberships and has_many :houses, :through => :memberships. But I got an error each time I try to access house from User.
How is it possible to set up this one-to-many relation with a join table like I was trying to do ?
Thank you in advance.
If you are going to use the one to many relation then association should be.
House
has_many :memberships
has_many :users, :through => :memberships
User
has_one :membership
has_one :house, :through => :membership
I can't think of a way doing this with relations, however you could leave it as the plural and then just define house in users:
class User
has_many :memberships
has_many :houses, :through => :memberships
def house
houses.first
end
end