Rails3: Devise User has_one relationship

谁说我不能喝 提交于 2019-12-13 08:35:59

问题


I have a Devise generated User model which has_one Team. I am trying to accomplish 3 things: 1. Allow people who have not registered or signed in to create a Team 2. Pass this team on during registration to link a User to a Team 3. Recall this Team whenever the User signs in.

Here's what I have right now:

Team.rb

has_many :members, :dependent => :destroy
belongs_to :user

User.rb

has_one :team
attr_accessible #devise stuff, :team
accepts_nested_attributes_for :team

I've generated and migrated the :user_id column for the Team table.

One portion of my code was based off of working through Agile Web Dvpt. 4th Ed. which left me with the following for finding my team:

ApplicationsController

def current_team
    Team.find(session[:team_id])
rescue ActiveRecord::RecordNotFound
    team = Team.create
    session[:team_id] = team.id
    team
end

I tried messing with this method to come up with the functionality I wanted, but it felt like this wasn't the right place to have this stuff.

Through searching stackoverflow, there seems to be a number of different suggestions. I have tried a number of them to no avail, and would like clarification on what the correct path forward is. I feel like I should be overriding the Devise RegistrationsController with my own to build the Team along with the User. If user_signed_in?, the current_team method should be grabbing the team from the User, else creating a Team that can be potentially saved in Registration.

Any guidance on this is greatly appreciated!

来源:https://stackoverflow.com/questions/6400363/rails3-devise-user-has-one-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!