Rails Trying to create a profile after the user has been created - ruby-on-rails

I'm trying to figure out how to create a new profile for the user that has just been created,
I'm using devise on the User model, and the User model has a one to one relationship with the UserProfile model.
Here's what my User Model looks like:
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
belongs_to :batch
has_one :user_profile
after_create :create_user_profile
def create_user_profile
self.user_profile.new(:name => 'username')
end
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
This generate the following error:
undefined method `new' for nil:NilClass
i've tried User.find(1).user_profile in rails c and that works so I'm pritty sure the relationship is setup correctly,
I'm probably being a big fat noob and trying to fetch self incorrectly.
plus can you also tell me how to access the params in a Model... is that even possible?

A has_one relationship will automatically add the :create_<relationship> and build_<relationship> methods. Checkout the rails guide.
You could try this:
after_create do
create_user_profile(:name => 'username')
end
Or more likely
after_create do
create_user_profile(:name => self.username)
end

Related

`method_missing': undefined method `devise_modules'

I'm trying to separate models from an RoR app into a gem. I'm getting an error when I extended the User model from the gem for adding Devise instance methods
I've tried different methods
User.class_eval and ModelsGem::User.class_eval
Single table inheritance like: class SuperClass < ModelsGem::User; end
Overriding the model class like class User < ActiveModel::Base
None of them worked with devise.. However, I could access methods of User model from the gem in the app and everything is working as expected other than devise.
You can do something like this
rails g devise:views
rails g devise user
if we wanna to add sth like first name and last name put it in the db before rake db:migrate
t.string :first_name
t.string :last_name
then rake db:migrate
in the user model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable, :confirmable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
end
I hope I did solve your problem

attr_accessor does not work on associated objects that aren't saved

I haven't been able to find an explanation as to why attr_accessor works here:
fb_profile = FbProfile.where(identifier: params[:identifier]).first_or_initialize
fb_profile.signed_request = "randomstring"
logger.info(fb_profile.signed_request) # outputs "randomstring"
but not here:
current_admin.fb_profile = FbProfile.where(identifier: params[:identifier]).first_or_initialize
current_admin.fb_profile.signed_request = "randomstring"
logger.info(current_admin.fb_profile.signed_request) # outputs undefined
I can work like in the first example, but then I have to create the association later, which seems dirtier than just creating it from the get go. Is this common behavior?
Update1:
As some requested, here are the relevant parts of both models:
class Admin < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :omniauthable
include DeviseTokenAuth::Concerns::User
has_one :fb_profile
end
class FbProfile < ApplicationRecord
belongs_to :admin
has_and_belongs_to_many :fb_pages
attr_accessor :signed_request
end
You're missing the relation between current_admin and fbprofile.
If you've a has_one relation I would write something like this :
current_admin.fb_profile ||= current_admin.build_fb_profile(identifier: params[:identifier])

Undefined method in Rails console when adding role to user

I am trying to add a role to a specific user in my Rails Console via Heroku , but I am getting the error:
NoMethodError: undefined method `role=' for # .
If you take a look at the screenshot below the role you can see all of the columns in the User table that is available:
The command I am using from the rails console is below to assign the role to the user. I saw a similar post on StackOverflow about the same error but after the db migrate I still got the same error. Can someone point me in the right direction possibly?
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable
has_many :posts
def admin?
role == 'admin'
end
def moderator?
role == 'moderator'
end
end

Get variable from another table in ROR models

I am trying to get a user who has not filled out their profile to redirect to the edit page.
I have two tables - Users (Devise handles) and Profiles.
Profiles has a row called profile_complete
In my user model I am trying to define a method called profile_complete which takes the boolean of profiles_complete and passes it back.
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :profiles, :dependent => :destroy
def profile_complete?
self.profile.profile_complete == true
end
end
But cannot seem to figure out what the line in the profile_complete method is. I have got the other bits working in the correct place but cant get this variable across Any help? Cheers :)
def profile_complete?
self.attributes.values.include?(nil) ? false : true
end

retrieve object id from relation in rails3

i have a model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :meetings, :dependent => :destroy do
def find_foreign
Meeting.where("user_id <> ?", id)
end
end
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
end
and when i am trying to get user's foreign meetings like that
some_user.meetings.find_foreign
i get an error
NoMethodError (undefined method `id' for []:ActiveRecord::Relation):
because self in find_foreign is an Array. How to retrive the User.id from this method ?
you can access self here:
def find_foreign
Meeting.where("user_id <> ?", self.id)
end
But don't know why you've written this?
some_user.meetings will already filter meetings by current user id. I've even no idea if block is allowed here!
find_foreign method is in User Model and you are trying to call it on Array of the Object of the Meetings
Just use
some_user.find_foreign

Resources