user5527042
user5527042

Reputation:

Create User Account Settings Page in Ruby on Rails with devise

I am new to Ruby on Rails and I have created a project that contains a User table (generated by devise) and a AccountSetting table that contains user specific account settings (this table has a foreign key that relates to the id in the User table thus each User has zero or one AccountSettings). I have my seed data working fine, and I can seed the database with users that have user specific account settings. The User table is related to the AccountSetting table with a "has_one :accountsetting" and the AccountSettings table "belongs_to :user". This all works and makes sense. However, I have a method called "show_user_setting" in my UserSettings controller, and I do not know how to ONLY SHOW the account settings for that specific authenticated user.

So, how can I only display the user setting for the currently logged in user? Again, I am using devise. My general idea of how to do this would be something like this. However I know this is incorrect, but for the purpose of an explanation, here it is.

def show_user_setting
     @setting = AccountSetting.find(current_user)
end

My idea is that the @setting will contain the setting for the currently logged in user. Thanks in advance!

Upvotes: 3

Views: 1614

Answers (3)

Richard Peck
Richard Peck

Reputation: 76784

You should do this:

#app/models/account_setting.rb
class AccountSetting < ActiveRecord::Base
   belongs_to :user
end

#app/models/user.rb
class User < ActiveRecord::Base
   has_one :account_setting
end

This will allow you to call the following:

@setting = current_user.account_setting

Our Setup

For what it's worth, we do something similar:

#app/models/user.rb
class User < ActiveRecord::Base
   before_create :build_profile #-> builds a blank profile on user create
   has_one :profile
end

#app/models/profile.rb
class Profile < ActiveRecord::Base
   belongs_to :user
end

This allows us to put all sorts of different options inside the profile model (we have homepage etc):

enter image description here

The important thing to note here is that the above allows you to delegate various methods to the profile model, allowing you to call the following:

current_user.profile_name
current_user.profile_signin_redirect?
current_user.profile_avatar

etc

Upvotes: 3

robertoplancarte
robertoplancarte

Reputation: 1181

I'm guessing the show_user_setting function is part of a controller, if it is on a model then read this: accessing devise current_user within model

to set the @setting variable you should be able to do this

 @setting = AccountSetting.find(user_id: current_user.id)

or

@setting = AccountSetting.find(user: current_user)

Upvotes: -1

user2826911
user2826911

Reputation: 44

Have you tried

def show_user_setting
   @setting = AccountSetting.find_by(user_id: current_user.id)
end

The way .find() works is it searches the model for the id passed. So, the way you currently have it is your going to try to search for the id of the model, when you want to find the foreign key. So use Model.find_by(column_name: param). You'll what to change user_id: to the column name of what you're storing the foreign key in, I'm just assuming it's something similar to that.

Upvotes: -1

Related Questions