Ruby on rails associations - User (devise) can 'create a team' or 'join team' after sign up and log in












-1















Hey guys im working on a rails application where users that have successfully signed up and logged in to their dashboard can create a team and become the leader (invite others to join via email, delete existing members etc.) or join an existing team as a member.( can only view other members and information about themselves) I'm using the Devise gem for user login / sign up since it has a lot of what i need. A team has one manager and has many members. Heres what ive tried, users belong to teams, and teams have many users. Ive also tried the association, Users have one team and teams have many users but one manager. I'm very confused thanks again.



user.rb



class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :confirmable
validates_presence_of :phone, :city, :state, :street, :zip, presence: true, on: :create
has_one :team
end


schema.rb



  create_table "teams", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "team_name"
t.string "team_statement"
t.bigint "user_id"
t.index ["user_id"], name: "index_teams_on_user_id"
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "firstname"
t.string "middlename"
t.string "lastname"
t.string "phone"
t.string "city"
t.string "state"
t.string "street"
t.string "zip"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end


team.rb



class Team < ApplicationRecord
belongs_to :user, class_name: 'User'
end


routes.rb



   devise_for :users, path: 'users' , controllers: { sessions: "users/sessions", confirmations: 'users/confirmations', registrations: 'users/registrations' } 









share|improve this question

























  • There is no one question. What problem do you solve?

    – Neodelf
    Nov 22 '18 at 19:15











  • My question is what is the best way of handling this situation, ive provided the code above to show what ive tried. Ive described what my issues are. @Neodelf

    – kevin lopez
    Nov 22 '18 at 19:18













  • It depends on what screens do you have. It can be a form on the signup page (where user immediately choose what he or she wants - becomes a leader of a team or create a new one) or page after successfully login/signup process.

    – Neodelf
    Nov 22 '18 at 19:26











  • @Neodelf Thanks for the quick reply, the users choose whether they would like to Create a new team and become the leader, or join an existing team after sign up and logged in

    – kevin lopez
    Nov 22 '18 at 19:32











  • Just you should create a new page (route, controller, view and action) and create form. Did you try it?

    – Neodelf
    Nov 22 '18 at 19:36


















-1















Hey guys im working on a rails application where users that have successfully signed up and logged in to their dashboard can create a team and become the leader (invite others to join via email, delete existing members etc.) or join an existing team as a member.( can only view other members and information about themselves) I'm using the Devise gem for user login / sign up since it has a lot of what i need. A team has one manager and has many members. Heres what ive tried, users belong to teams, and teams have many users. Ive also tried the association, Users have one team and teams have many users but one manager. I'm very confused thanks again.



user.rb



class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :confirmable
validates_presence_of :phone, :city, :state, :street, :zip, presence: true, on: :create
has_one :team
end


schema.rb



  create_table "teams", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "team_name"
t.string "team_statement"
t.bigint "user_id"
t.index ["user_id"], name: "index_teams_on_user_id"
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "firstname"
t.string "middlename"
t.string "lastname"
t.string "phone"
t.string "city"
t.string "state"
t.string "street"
t.string "zip"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end


team.rb



class Team < ApplicationRecord
belongs_to :user, class_name: 'User'
end


routes.rb



   devise_for :users, path: 'users' , controllers: { sessions: "users/sessions", confirmations: 'users/confirmations', registrations: 'users/registrations' } 









share|improve this question

























  • There is no one question. What problem do you solve?

    – Neodelf
    Nov 22 '18 at 19:15











  • My question is what is the best way of handling this situation, ive provided the code above to show what ive tried. Ive described what my issues are. @Neodelf

    – kevin lopez
    Nov 22 '18 at 19:18













  • It depends on what screens do you have. It can be a form on the signup page (where user immediately choose what he or she wants - becomes a leader of a team or create a new one) or page after successfully login/signup process.

    – Neodelf
    Nov 22 '18 at 19:26











  • @Neodelf Thanks for the quick reply, the users choose whether they would like to Create a new team and become the leader, or join an existing team after sign up and logged in

    – kevin lopez
    Nov 22 '18 at 19:32











  • Just you should create a new page (route, controller, view and action) and create form. Did you try it?

    – Neodelf
    Nov 22 '18 at 19:36
















-1












-1








-1








Hey guys im working on a rails application where users that have successfully signed up and logged in to their dashboard can create a team and become the leader (invite others to join via email, delete existing members etc.) or join an existing team as a member.( can only view other members and information about themselves) I'm using the Devise gem for user login / sign up since it has a lot of what i need. A team has one manager and has many members. Heres what ive tried, users belong to teams, and teams have many users. Ive also tried the association, Users have one team and teams have many users but one manager. I'm very confused thanks again.



user.rb



class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :confirmable
validates_presence_of :phone, :city, :state, :street, :zip, presence: true, on: :create
has_one :team
end


schema.rb



  create_table "teams", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "team_name"
t.string "team_statement"
t.bigint "user_id"
t.index ["user_id"], name: "index_teams_on_user_id"
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "firstname"
t.string "middlename"
t.string "lastname"
t.string "phone"
t.string "city"
t.string "state"
t.string "street"
t.string "zip"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end


team.rb



class Team < ApplicationRecord
belongs_to :user, class_name: 'User'
end


routes.rb



   devise_for :users, path: 'users' , controllers: { sessions: "users/sessions", confirmations: 'users/confirmations', registrations: 'users/registrations' } 









share|improve this question
















Hey guys im working on a rails application where users that have successfully signed up and logged in to their dashboard can create a team and become the leader (invite others to join via email, delete existing members etc.) or join an existing team as a member.( can only view other members and information about themselves) I'm using the Devise gem for user login / sign up since it has a lot of what i need. A team has one manager and has many members. Heres what ive tried, users belong to teams, and teams have many users. Ive also tried the association, Users have one team and teams have many users but one manager. I'm very confused thanks again.



user.rb



class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :confirmable
validates_presence_of :phone, :city, :state, :street, :zip, presence: true, on: :create
has_one :team
end


schema.rb



  create_table "teams", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "team_name"
t.string "team_statement"
t.bigint "user_id"
t.index ["user_id"], name: "index_teams_on_user_id"
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "firstname"
t.string "middlename"
t.string "lastname"
t.string "phone"
t.string "city"
t.string "state"
t.string "street"
t.string "zip"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end


team.rb



class Team < ApplicationRecord
belongs_to :user, class_name: 'User'
end


routes.rb



   devise_for :users, path: 'users' , controllers: { sessions: "users/sessions", confirmations: 'users/confirmations', registrations: 'users/registrations' } 






ruby-on-rails ruby devise associations






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 20:33







kevin lopez

















asked Nov 22 '18 at 18:55









kevin lopezkevin lopez

336




336













  • There is no one question. What problem do you solve?

    – Neodelf
    Nov 22 '18 at 19:15











  • My question is what is the best way of handling this situation, ive provided the code above to show what ive tried. Ive described what my issues are. @Neodelf

    – kevin lopez
    Nov 22 '18 at 19:18













  • It depends on what screens do you have. It can be a form on the signup page (where user immediately choose what he or she wants - becomes a leader of a team or create a new one) or page after successfully login/signup process.

    – Neodelf
    Nov 22 '18 at 19:26











  • @Neodelf Thanks for the quick reply, the users choose whether they would like to Create a new team and become the leader, or join an existing team after sign up and logged in

    – kevin lopez
    Nov 22 '18 at 19:32











  • Just you should create a new page (route, controller, view and action) and create form. Did you try it?

    – Neodelf
    Nov 22 '18 at 19:36





















  • There is no one question. What problem do you solve?

    – Neodelf
    Nov 22 '18 at 19:15











  • My question is what is the best way of handling this situation, ive provided the code above to show what ive tried. Ive described what my issues are. @Neodelf

    – kevin lopez
    Nov 22 '18 at 19:18













  • It depends on what screens do you have. It can be a form on the signup page (where user immediately choose what he or she wants - becomes a leader of a team or create a new one) or page after successfully login/signup process.

    – Neodelf
    Nov 22 '18 at 19:26











  • @Neodelf Thanks for the quick reply, the users choose whether they would like to Create a new team and become the leader, or join an existing team after sign up and logged in

    – kevin lopez
    Nov 22 '18 at 19:32











  • Just you should create a new page (route, controller, view and action) and create form. Did you try it?

    – Neodelf
    Nov 22 '18 at 19:36



















There is no one question. What problem do you solve?

– Neodelf
Nov 22 '18 at 19:15





There is no one question. What problem do you solve?

– Neodelf
Nov 22 '18 at 19:15













My question is what is the best way of handling this situation, ive provided the code above to show what ive tried. Ive described what my issues are. @Neodelf

– kevin lopez
Nov 22 '18 at 19:18







My question is what is the best way of handling this situation, ive provided the code above to show what ive tried. Ive described what my issues are. @Neodelf

– kevin lopez
Nov 22 '18 at 19:18















It depends on what screens do you have. It can be a form on the signup page (where user immediately choose what he or she wants - becomes a leader of a team or create a new one) or page after successfully login/signup process.

– Neodelf
Nov 22 '18 at 19:26





It depends on what screens do you have. It can be a form on the signup page (where user immediately choose what he or she wants - becomes a leader of a team or create a new one) or page after successfully login/signup process.

– Neodelf
Nov 22 '18 at 19:26













@Neodelf Thanks for the quick reply, the users choose whether they would like to Create a new team and become the leader, or join an existing team after sign up and logged in

– kevin lopez
Nov 22 '18 at 19:32





@Neodelf Thanks for the quick reply, the users choose whether they would like to Create a new team and become the leader, or join an existing team after sign up and logged in

– kevin lopez
Nov 22 '18 at 19:32













Just you should create a new page (route, controller, view and action) and create form. Did you try it?

– Neodelf
Nov 22 '18 at 19:36







Just you should create a new page (route, controller, view and action) and create form. Did you try it?

– Neodelf
Nov 22 '18 at 19:36














1 Answer
1






active

oldest

votes


















0














You just add integer field leader_id to teams table. Also, you should add this row to team.rb:



belongs_to :leader, class_name: 'User'


that helps you find a leader from a team (like team.leader).
You should not use has_and_belongs_to_many association.






share|improve this answer
























  • awesome this is currently working but how will but how will i add more users to teams as members?

    – kevin lopez
    Nov 22 '18 at 20:18











  • just use added field team_id on users table. For example: You have records: User id: 1, team_id: 1, ... ; Team id: 1, leader_id: 1. With them you can use team.users, team.leader and user.team methods

    – Neodelf
    Nov 22 '18 at 20:23













  • Bare with me i have updated the code above, am i still doing something wrong? @Neodelf

    – kevin lopez
    Nov 22 '18 at 20:37











  • Add the code above and methods in the comment above will work

    – Neodelf
    Nov 22 '18 at 20:45











  • i have updated the code in my original question above, am i still doing something wrong ? @Neodelf

    – kevin lopez
    Nov 22 '18 at 20:59













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436740%2fruby-on-rails-associations-user-devise-can-create-a-team-or-join-team-af%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You just add integer field leader_id to teams table. Also, you should add this row to team.rb:



belongs_to :leader, class_name: 'User'


that helps you find a leader from a team (like team.leader).
You should not use has_and_belongs_to_many association.






share|improve this answer
























  • awesome this is currently working but how will but how will i add more users to teams as members?

    – kevin lopez
    Nov 22 '18 at 20:18











  • just use added field team_id on users table. For example: You have records: User id: 1, team_id: 1, ... ; Team id: 1, leader_id: 1. With them you can use team.users, team.leader and user.team methods

    – Neodelf
    Nov 22 '18 at 20:23













  • Bare with me i have updated the code above, am i still doing something wrong? @Neodelf

    – kevin lopez
    Nov 22 '18 at 20:37











  • Add the code above and methods in the comment above will work

    – Neodelf
    Nov 22 '18 at 20:45











  • i have updated the code in my original question above, am i still doing something wrong ? @Neodelf

    – kevin lopez
    Nov 22 '18 at 20:59


















0














You just add integer field leader_id to teams table. Also, you should add this row to team.rb:



belongs_to :leader, class_name: 'User'


that helps you find a leader from a team (like team.leader).
You should not use has_and_belongs_to_many association.






share|improve this answer
























  • awesome this is currently working but how will but how will i add more users to teams as members?

    – kevin lopez
    Nov 22 '18 at 20:18











  • just use added field team_id on users table. For example: You have records: User id: 1, team_id: 1, ... ; Team id: 1, leader_id: 1. With them you can use team.users, team.leader and user.team methods

    – Neodelf
    Nov 22 '18 at 20:23













  • Bare with me i have updated the code above, am i still doing something wrong? @Neodelf

    – kevin lopez
    Nov 22 '18 at 20:37











  • Add the code above and methods in the comment above will work

    – Neodelf
    Nov 22 '18 at 20:45











  • i have updated the code in my original question above, am i still doing something wrong ? @Neodelf

    – kevin lopez
    Nov 22 '18 at 20:59
















0












0








0







You just add integer field leader_id to teams table. Also, you should add this row to team.rb:



belongs_to :leader, class_name: 'User'


that helps you find a leader from a team (like team.leader).
You should not use has_and_belongs_to_many association.






share|improve this answer













You just add integer field leader_id to teams table. Also, you should add this row to team.rb:



belongs_to :leader, class_name: 'User'


that helps you find a leader from a team (like team.leader).
You should not use has_and_belongs_to_many association.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 19:57









NeodelfNeodelf

91113




91113













  • awesome this is currently working but how will but how will i add more users to teams as members?

    – kevin lopez
    Nov 22 '18 at 20:18











  • just use added field team_id on users table. For example: You have records: User id: 1, team_id: 1, ... ; Team id: 1, leader_id: 1. With them you can use team.users, team.leader and user.team methods

    – Neodelf
    Nov 22 '18 at 20:23













  • Bare with me i have updated the code above, am i still doing something wrong? @Neodelf

    – kevin lopez
    Nov 22 '18 at 20:37











  • Add the code above and methods in the comment above will work

    – Neodelf
    Nov 22 '18 at 20:45











  • i have updated the code in my original question above, am i still doing something wrong ? @Neodelf

    – kevin lopez
    Nov 22 '18 at 20:59





















  • awesome this is currently working but how will but how will i add more users to teams as members?

    – kevin lopez
    Nov 22 '18 at 20:18











  • just use added field team_id on users table. For example: You have records: User id: 1, team_id: 1, ... ; Team id: 1, leader_id: 1. With them you can use team.users, team.leader and user.team methods

    – Neodelf
    Nov 22 '18 at 20:23













  • Bare with me i have updated the code above, am i still doing something wrong? @Neodelf

    – kevin lopez
    Nov 22 '18 at 20:37











  • Add the code above and methods in the comment above will work

    – Neodelf
    Nov 22 '18 at 20:45











  • i have updated the code in my original question above, am i still doing something wrong ? @Neodelf

    – kevin lopez
    Nov 22 '18 at 20:59



















awesome this is currently working but how will but how will i add more users to teams as members?

– kevin lopez
Nov 22 '18 at 20:18





awesome this is currently working but how will but how will i add more users to teams as members?

– kevin lopez
Nov 22 '18 at 20:18













just use added field team_id on users table. For example: You have records: User id: 1, team_id: 1, ... ; Team id: 1, leader_id: 1. With them you can use team.users, team.leader and user.team methods

– Neodelf
Nov 22 '18 at 20:23







just use added field team_id on users table. For example: You have records: User id: 1, team_id: 1, ... ; Team id: 1, leader_id: 1. With them you can use team.users, team.leader and user.team methods

– Neodelf
Nov 22 '18 at 20:23















Bare with me i have updated the code above, am i still doing something wrong? @Neodelf

– kevin lopez
Nov 22 '18 at 20:37





Bare with me i have updated the code above, am i still doing something wrong? @Neodelf

– kevin lopez
Nov 22 '18 at 20:37













Add the code above and methods in the comment above will work

– Neodelf
Nov 22 '18 at 20:45





Add the code above and methods in the comment above will work

– Neodelf
Nov 22 '18 at 20:45













i have updated the code in my original question above, am i still doing something wrong ? @Neodelf

– kevin lopez
Nov 22 '18 at 20:59







i have updated the code in my original question above, am i still doing something wrong ? @Neodelf

– kevin lopez
Nov 22 '18 at 20:59




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436740%2fruby-on-rails-associations-user-devise-can-create-a-team-or-join-team-af%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft