I can't wrap my head around this any help would be appreciated. I went through many articles and other postings on here and I can't seem to get the results I'm looking for.
I have a User model and Team model.
Team Model has user_id and team_id
The user who creates the team will be user_id and users who are members of the team will be in the team_id
User Model
has_many :teams, foreign_key: :team_id
has_many :team_members, class_name: "Team", foreign_key: :user_id
Team Model
belongs_to :user, foreign_key: :team_id
belongs_to :team_member, class_name: "User", foreign_key: :user_id
The end result of what I'm trying to achieve is:
Each user can add as many team members
Each user can see a list of users who are part of their team.
A view where, Users who are part of a team can see which team they are part of.
I believe what you're looking for is a join table model. The issue is that both a User and a Team may have many of each other. So the relationship must be stored separately.
See this answer here on a similar question: https://stackoverflow.com/a/15442583/5113832
So you might choose a model structure of User, Team and TeamMembership.
Updated to destroy dependent team memberships when a user or team is destroyed.
#app/models/user.rb
class User < ActiveRecord::Base
has_many :team_memberships, :dependent => :destroy
has_many :teams, :through => :team_memberships
end
#app/models/team.rb
class Team < ActiveRecord::Base
has_many :team_memberships, :dependent => :destroy
has_many :users, :through => :team_memberships
end
#app/models/team_membership.rb
class TeamMembership < ActiveRecord::Base
belongs_to :user
belongs_to :team
end
Updated to reply to question:
How would my controller look on create? (Adding a user to a team) – Michael
In order to add a user to to a team you COULD do it in UserController, you COULD do it in TeamController. However, because you are now creating a TeamMembership resource you would want to create a TeamMembership record in a TeamMembershipsController. This keeps with the "Rails" way of doing things. So for example:
# app/controllers/team_memberships_controller.rb
class TeamMembershipsController < ApplicationController
def index
#team_memberships = TeamMembership.all
end
def new
#team_membership = TeamMembership.new
end
def create
#team_membership = TeamMembership.new(team_membership_params)
if #team_membership.save
flash[:success] = 'Team membership created'
redirect_to team_memberships_path
else
flash[:error] = 'Team membership not created'
render :new
end
end
def destroy
#team_membership = TeamMembership.find_by_id(params[:id])
if #team_membership && #team_membership.destroy
flash[:success] = 'Team membership destroyed'
else
flash[:error] = 'Team membership not destroyed'
end
redirect_to team_memberships_path
end
private
def team_membership_params
params.require(:team_membership).permit(
:user_id,
:team_id
)
end
end
The advantage to having the TeamMembership resource is using this pattern to manage when a user is added (#create), or removed (#destroy) from a team.
The magic of Rails associations will take care of accessing a team's members (users) and a user's teams for each instance of those models.
You just go about your business doing CRUD on these resources and Rails takes care of the organization for you by your conforming to it's conventions.
Also I updated my original model code to destroy team memberships when a user or team is destroyed. This ensures no orphaned records are in your team_memberships table.
As a final note. You should also be able to easily use form_for to send a TeamMembership to your controller to be created. This could be done using select option dropdowns for users and teams with Rails' collection_select:
<%# app/views/team_memberships/new.html.erb %>
<h1>
Create Team Membership
</h1>
<%= form_for(#team_membership) do |f| %>
<%= f.label(:user) %>
<%= f.collection_select(
:user_id,
User.all,
:id,
:username
) %>
<%= f.label(:team) %>
<%= f.collection_select(
:team_id,
Team.all,
:id,
:name
) %>
<%= f.submit %>
<% end %>
The above code will render dropdown for all users and teams allowing you to select a specific combination to create a team membership from. Deleting a team membership is as easy as sending a DELETE #destroy request with the id of the team membership.
Another consideration might be adding a unique pair constraint to your database table and model within the migration and model validations.
Hope this helps!
Related
I'm looking for the easiest and the most clever way to create interest_id(match) in one-click.
Here is my MVC :
user.rb
class User < ApplicationRecord
has_many :interests, through: :opportunities
end
interest.rb
class Interest < ApplicationRecord
belongs_to :opportunity
belongs_to :user
end
opportunity.rb
class Opportunity < ApplicationRecord
has_many :interests
end
InterestsController.rb
def create
#user = current_user
#opportunities = Opportunity.all
#interest = Interest.new(interest_params)
if #interest.save!
redirect_to user_interests_path, notice: 'it works'
else
render :new, notice:"it doesn't work"
end
end
def interest_params
params.permit(
:user_id,
:opportunity_id)
end
user/show
<%= link_to "Match", user_interests_path(#user), class:"btn btn-primary", :method => :post %>
For now, I can't pass opportunities (nil). Could you please advise me about the easiest way to create interests? (New on RoR for 6 months).
Many thanks for your help.
If I understand correctly your relation schema, the Interest is the join record associating a User to (eventually) many Opportunity, and vice-versa (many-to-many relationship).
With that being said (and please correct me if I am wrong), you can do the following to achieve what you want:
# in user/show
<% #opportunities.each do |opportunity| %>
<%=
link_to "Match opportunity #{opportunity.id}",
user_interests_path(#user, opportunity_id: opportunity.id),
class: "btn btn-primary",
method: :post
%>
<% end %>
# in interests_controller
def create
if current_user.interests.create(opportunity_id: opportunity_id_param)
redirect_to user_interests_path, notice: 'it works'
else
render :new, notice: "it doesn't work"
end
end
private
def opportunity_id_param
params.require(:opportunity_id)
end
This suggested code:
requires the opportunity_id param for the interests#create action
use current_user to automatically set the user_id on the Interest model, so the end-users can't send a user_id that are not theirs (if they could, then each user could create interest for other users without their agreement... security flaw)
On a side note, I strongly advise you to not select all existing Opportunity record and display it on your page: it does not scale. Someday, you will end up with hundreds of Opportunity records, making this list too big from a User Experience perspective.
I suggest a smarter approach, for example some kind of ordering + limit: max of 10 records ordered by "most interest", which can be accomplished by the following:
# in controller
#popular_opportunities = Opportunity
.joins('LEFT JOINS interests ON interests.opportunity_id = opportunities.id')
.order('count(interests.*) DESC, opportunities.id')
.limit(10)
And then in the view, simply use #populator_opportunities instead of #opportunities.
Other options, like pagination, are also efficient in this case but IMO relevant ordering is the minimum.
First, you need to pass the ids of the opportunities you want to create interest some way, the best is a form, with checkboxes like MrShemek said, or a multi select dropdown.
I think you probably made some mistakes in User and Opportunity with the has_many and belong_to part:
class User < ApplicationRecord
has_many :interests
has_many :opportunities, through: :interests
# interest is the one that links user and opportunity, it has the references for both user and opportunities
end
class Opportunity < ApplicationRecord
has_many :interests
has_many :users, through: :interests
end
then in controller you could do
def create
#user = current_user
#opportunities = Opportunity.all
#user.opportunity_ids = interest_params[:opportunity_ids] # it will create the interrests automatically for the given ids (because the relations of has_many through)
if #user.save!
redirect_to user_interests_path, notice: 'it works'
else
render :new, notice:"it doesn't work"
end
end
I have a users and a teams table. The users table has a column called team_id. But now the goal is to allow users to have multiple teams.
So I created the user_teams table using migrations like so:
class CreateUserteams < ActiveRecord::Migration[5.0]
def change
create_table :user_teams do |t|
t.integer :user_id
t.integer :team_id
t.timestamps
end
end
end
And my code is as follows:
models/user.rb
class User < ApplicationRecord
#belongs_to :team
# should now have multiple teams
has_many :user_teams
has_many :teams, through: :user_teams
models/team.rb
class Team < ApplicationRecord
has_many :members, foreign_key: "team_id", class_name: "User"
has_many :user_teams
has_many :users, through: :user_teams
models/user_teams.rb (new file)
class UserTeam < ApplicationRecord
belongs_to :user
belongs_to :team
end
(This works fine) To invite a user to join a team I send him an invite and if he accepts, I add him to the new users_team table using:
controllers/team_invites_controller.br
def accept
user = #team_invite.user
#team_invite.update(status: TeamInvite.statuses[:accepted])
user.team_id = #team_invite.team_id
user.company_id = #team_invite.team.company_id
user.teams << Team.find(user.team_id)
user.save(:validate => false)
respond_to do |format|
if user.shadow?
format.html {
redirect_to new_user_registration_path({
:email => user.email,
:role => user.main_role,
})
}
else
format.html { redirect_to dashboard_root_path, notice: 'Invite accepted' }
end
format.json { head :no_content }
end
end
The problem is, now "user" somehow became a Team and not a User, and it broke all of the code using "user.something". Also because of this I cannot access User methods because "user" is no longer a User but a Team.
For example, now:
<% if user.photo.exists? %>
gives:
undefined method `photo' for Team:0x00007f381dea4708
Or:
<% if current_user.teams.members.count > 1 %>
gives:
undefined method `members' for
Team::ActiveRecord_Associations_CollectionProxy:0x00007f3845954eb0 Did you mean? member?
Why does users.something now behaves as a team? I already re-did migrations and got the same error. Any idea how thatvhappened and how to fix it? How can I access users properties and methods correctly?
Thank you
EDIT
On
<% if user.photo.exists? %>
it's meant to display the image of another user and it gives the undefined method `photo' error. But if I use (although I don't want to)
<% if current_user.photo.exists? %>
It works. How come?
Your users have multiple teams and thus current_user.teams is not a Team anymore, it's a collection proxy, to access individual teams from there, depending on your logic you can use:
<% if current_user.teams.any?{|team| team.members.count > 1 } %>
(note that this code may be inefficient if there're many teams)
I have a marketplace where my users can create plans and their customers can join them. So I have a Plan model and a Customer model. The end goal is to subscribe a customer to a plan so I created a Subscription model and a has_many :through association but I need some help on getting the create working properly. A plan and a customer are already existing by the time the subscription is able to happen so I don't need to worry about creating the plan or customer on subscription#create, I just need to worry about joining the existing ones.
Where I'm at right now is I got the create working on the subscriptions model, but it's not associating to the correct customer. I need a Subscription model created for every customer that I subscribe to the plan and I'm using a multi select tag.
I'm using a has_many :through because a plan has many customers but a customer can also have many plans.
Please let me know if anything is not clear I tried to explain it as clearly and concisely as possible.
Plan Model
class Plan < ActiveRecord::Base
has_many :subscriptions
has_many :customers, through: :subscriptions
end
Customer Model
class Customer < ActiveRecord::Base
has_many :subscriptions
has_many :plans, through: :subscriptions, dependent: :delete_all
end
Subscription Model
class Subscription < ActiveRecord::Base
belongs_to :plan
belongs_to :customer
end
Routes.rb
resources :customers
resources :plans do
resources :subscriptions
end
Subscriptions Controller
class SubscriptionsController < ApplicationController
def new
#user = current_user
#company = #user.company
#plan = Plan.find(params[:plan_id])
#subscription = Subscription.new
end
def create
if #subscription = Subscription.create(plan_id: params[:subscription][:plan_id] )
#subscription.customer_id = params[:subscription][:customer_id]
#subscription.save
flash[:success] = "Successfully Added Customers to Plan"
redirect_to plan_path(params[:subscription][:plan_id])
else
flash[:danger] = "There was a problem adding your customers to this plan"
render :new
end
end
private
def subscription_params
params.require(:subscription).permit(:customer_id, :plan_id, :stripe_subscription_id)
end
end
Form:
<%= form_for [#plan, #subscription] do |f| %>
<%= f.hidden_field :plan_id, value: #plan.id %>
<div class="row">
<div class="col-md-6">
<%= f.select :customer_id, options_from_collection_for_select(#company.customers, 'id', 'customer_name', #plan.customers), {}, multiple: true, style: "width: 50%;" %><br />
</div>
<div class="col-md-12">
<%= f.submit "Add Customer To Plan", class: "btn btn-success pull-right" %>
</div>
</div>
<% end %>
params:
{"utf8"=>"✓",
"authenticity_token"=>"###",
"subscription"=>{"plan_id"=>"5", "customer_id"=>["", "153", "155"]},
"commit"=>"Add Customer To Plan",
"action"=>"create",
"controller"=>"subscriptions",
"plan_id"=>"5"}
params[:subscription][:customer_id] is an array:
"subscription"=>{"plan_id"=>"5", "customer_id"=>["", "153", "155"]},
Are you actually trying to set up a subscription between the plan and each of the customers in this array? If so try calling the update method for #plan object instead, passing these through in params[:plan][:customer_ids] (note the s)
EDIT:
When i said "pass through the ids in params[:plan][:customer_ids]" i was expecting you to do the standard controller behaviour for update, which is something along the lines of
#plan = Plan.find(params[:plan_id])
#plan.update_attributes(params[:plan])
if params = {:plan => {:customer_ids => [1,2,3]}} then the above code will be doing this:
#plan.update_attributes({:customer_ids => [1,2,3]})
which is like saying
#plan.customer_ids = [1,2,3]
#plan.save
When you set up an association, you get lots of methods you can call on the object. One of them is <association>_ids, in this case customer_ids, which is a way of setting the association: when you save, it will make the association between #plan and customers 1,2 & 3.
You were doing this:
#plan.customers << params[:plan][:customer_ids]
which is mixing up the customer records with the ids. If you're going to use push, aka <<, you need to push in customer objects, not ids. Just using customer_ids = is a quicker and simpler way of doing this.
I have a group model that has_many :members, and has_many :memberships. What I would like to do is make it so that in some groups the creator of the group would make it so that you have to request membership in order to join that specific group. How could I set this up in my rails application?
I have added a boolean field to the memberships ActiveRecord but I dont know how to set it up in a way that would allow me to join groups that dont require the "request a membership" function but also to create a "request a membership" function.
as of right now my models look like this:
class Group < ActiveRecord::Base
belongs_to :creator, :class_name => "User"
has_many :members, :through => :memberships
has_many :memberships, :foreign_key => "new_group_id"
has_many :events
end
class User < ActiveRecord::Base
has_many :groups, foreign_key: :creator_id
has_many :memberships, foreign_key: :member_id
has_many :new_groups, through: :memberships
end
class Membership < ActiveRecord::Base
belongs_to :member, class_name: "User"
belongs_to :new_group, class_name: "Group"
validates :new_group_id, uniqueness: {scope: :member_id}
has_many :accepted_memberships, -> {where(memberships: { approved: true}) }, :through => :memberships
has_many :pending_memberships, -> {where(memberships: { approved: false}) }, :through => :memberships
end
and my membership controller:
class MembershipsController < ApplicationController
def create
#group = Group.find(params[:new_group_id])
#membership = current_user.memberships.build(:new_group_id => params[:new_group_id])
if #membership.save
flash[:notice] = "Joined #{#group.name} "
else
flash[:notice] = "You're already in this group"
end
redirect_to groups_url
end
def destroy
#group = Group.find(params[:id])
#membership = current_user.memberships.find_by(params[membership_id: #group.id]).destroy
redirect_to groups_url
end
end
I believe that you are already very close to your solution, and that it is more of a business problem than a technical one. First I would add a boolean to the group to indicate that approval is required. e.g.
rails generate migration add_require_approval_to_groups require_approval:boolean
This would get set when the creator first creates the group depending upon the type of group that they have created.
Now, somehow a user has to be able to discover that there are groups that they can join, and you need to communicate an awareness to them that for some groups, membership is not automatic, but must be approved by the group creator.
So, assuming that you have communicated this to the user, and that they are on a page with a selection box listing all of the groups that they can become a member of (not necessarily the best design choice, but will do for this example). You need to have a query in your model that will gather all of the available groups that a user can still join.
def self.available_groups(user_id)
where("id not in (select group_id from group_members where user_id = ?)", user_id)
.select("id, name")
.collect{ |g| [g.name, g.id] }
end
In your controller:
#available_groups = Group.available_groups(#current_user)
And in your view:
<h2>Please select the group to join:</h2>
<p>
<%= form_tag :action => 'join_group' do %>
<%= select("group", "id",
#available_groups) %>
<%= submit_tag "Join" %>
<% end %>
</p>
Now, when you process the "post" in your membership_controller, you need to inform the creator that someone is trying to join the group that requires approval (perhaps a mailer). If the require_approval boolean is not set, then you need to automatically approve the user so that they can access the group immediately.
I want my users to have many skills. I do have a users and skills database table.
I used has_many_and_belongs_to association in user.rb
has_many :skills
which I am not sure if its correct. And in skill.rb
has_and_belongs_to_many :users
I also created a migration like that:
def change
create_table :user_skills do |t|
t.belongs_to :users
t.belongs_to :skills
end
Is this correct?
So IF this is correct, how do I add new skills to my user? What is the general approach?
What I thought of,
In my users controller on update action I will be updating user's skill and update the user_skills table.
How is this done?
Also How do I iterate through my user_skills table for a specific user? (in view)
Any guidance, resource, tip will be great help for me as its the first time i do something like this in Rails.
Thanks
In Rails, most would prefer to use has_many :through over habtm associations. Here's a guide on how to use it: ActiveRecord guide.
A has_many through association for users and skills would look like this in your relevant models:
class User < ActiveRecord::Base
has_many :user_skills
has_many :skills, through: :user_skills
end
class UserSkill < ActiveRecord::Base
belongs_to :user
belongs_to :skill
end
class Skill < ActiveRecord::Base
has_many :user_skills
has_many :users, through: :user_skills
end
Your migration would look like:
def change
create_table :user_skills do |t|
t.references :user, index: true
t.references :skill, index: true
end
end
The indexes in the migration are for faster look-ups for using the reference_id. It's advisable to do that for all references.
To add new skills to your user, you can refer to this SO answer.
To update a user's skill, you could do this:
#skill = #user.skills.find(params[:skill_id])
#skill.update(skill_params)
To create a user's skill, you could do this:
#user.skills.create(skill_params)
To add a skill to user, you could do this in your update action:
#user.update(user_params)
#app/views/users/edit.html.erb
<%= f.select :skill_ids, Skill.all.collect {|x| [x.name, x.id]}, {}, :multiple => true %>
When working with has_many through, you won't need to go through the user_skills table to get a specific user. You would, however, might need to get a specific user from a skill. To do this:
#skill.users.find(user_id)
Hope that helps!
If you set user to have_and_belong_to_many :skills also then this will work.
To create a new skill for a user do
user.skills.create!{...}
or to associate an existing skill with a user do
user << skill
"In my users controller on update action I will be updating user's skill and update the user_skills table. How is this done?"
user = User.find params[:id]
skills = user.skills
You can then do what you like to users skills
"Also How do I iterate through my user_skills table for a specific user? (in view)"
user.skills.each do |skill|
...
end
for more on HABTM association see http://guides.rubyonrails.org/association_basics.html#has-and-belongs-to-many-association-reference
Forgive me If I get it wrong, try to fill in the gaps but I think you want something that looks like this.
controller
def index
#to fetch all skills associated to users (add where u.id=? to fetch for a single user)
#users = User.select("u.name, s.name").
from("users u, skills s, users_skills us").
where("u.id = us.user_id").
where("s.id = us.skill_id")
end
def new
#user = User.new
#skills = Skill.all
end
def create
#user = User.new(params[:user])
...............................
end
in the create form
<%= form_for #user do |f| %>
<%= f.collection_select(:skill_ids, #skills,:id,:name)%>
<%= f.submit "Save" %>
<% end %>
In order to use HABTM you need a join table named either users_skills or skills_users (not sure it matters). It should contain two integer columns named user_id and skill_id. You should create indices for them as well. In your User model you want has_and_belongs_to_many :skills and in your Skill model you want has_and_belongs_to_many :users.
You need has_and_belongs_to_many on both sides of the realtionship.
class User
has_and_belongs_to_many :skills
class Skill
has_and_belongs_to_many :users
Alternatively (and better, in my opinion) would be to use has_many :through:
class User
has_many :user_skills
has_many :skills, through: :user_skills
class Skill
has_many :user_skills
has_many :users, through: :user_skills