following code is generated by devise.
How can we just use "devise" here to include the modules in my User class.
How is the devise recognized here ?
I don't see any include statement here. Is it included in the ActiveRecord ?
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
end
When you load Devise (probably via a Rails initializer), it extends the ActiveRecord::Base class with Devise::Model
See it in the devise source code.
ActiveRecord::Base.extend Devise::Models
Related
I'm trying to create a record for the associated model in rails console in the form Contact.first.to_do_items.create!() and I have ActiveRecord::RecordInvalid Validation failed: contact must exist
Models:
class Contact < ApplicationRecord
acts_as_token_authenticatable
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :to_do_items
end
class ToDoItem < ApplicationRecord
belongs_to :сontact
end
It works well when I'm using optional: true in the ToDoItem class but I'm curious why it's not creating a record with the code listed above.
I'm new in RoR. I'm learning models and generating the models for my app and it's associations. The console generated the error when I tried:
$ rails console
mypath.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activerecord-5.1.3/lib/active_record/associations.rb:1395:in
`has_many': wrong number of arguments (given 4, expected 1..3)
(ArgumentError)
and more lines of errors but this one is the key I think.
Solved it, I was writing on the same line different associations which is not correct:
Wrong:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :questions, :answers, :comments, :votes
end
Correct:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :questions
has_many :answers
has_many :comments
has_many :votes
end
I am new to stackoverflow and i want to implement user with multiple roles .
I had started with rolify gem . I had generated 3 devise users manager , owner , user (visitor).
Association used for my application is
class Role < ApplicationRecord
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource,
:polymorphic => true,
:optional => true
validates :resource_type,
:inclusion => { :in => Rolify.resource_types },
:allow_nil => true
scopify
end
class User < ApplicationRecord
rolify
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
after_create :assign_default_role
def assign_default_role
self.add_role(:visitor) if self.roles.blank?
end
end
class Owner < User
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
after_create :assign_default_role
def assign_default_role
self.add_role(:owner) if self.roles.blank?
end
end
class Manager < User
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
after_create :assign_default_role
def assign_default_role
self.add_role(:moderator) if self.roles.blank?
end
end
My concern is i am using rolify gem to assign role but i want to keep manager , owner , visitor table separate but if i didn't use Single table inheritance then how can i able to implement roles and keep table separate
I have a Devise User model with the following contents for which I did run migration.
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :token_authenticatable, :confirmable, :lockable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :role
# attr_accessible :title, :body
ROLES = ['admin', 'network/system admin', 'manager', 'programmer']
def role?(base_role)
ROLES.index(base_role.to_s) <= ROLES.index(role)
end
end
later on , I added the below two lines to the same model and run migration for Ticket, Projects and Assignments.
has_many :projects, :through => :assignments
has_many :tickets
Does the above update the association of user with the Tickets and Projects? Is there any problem in changing associations in model after running migration for the same? I want to know it as I am developing a Rails app now.
Thanks :)-
You should also have association...
has_many :assignments
in your user model.
no other updation is required.
I am using Devise in Rails 3, and have a User model in rails that is starting to get kinda crowded.. so I would like to put all of the login meethods inside of a module and include them from my model. I'm trying something like:
app/model/user.rb
class User < ActiveRecord::Base
include UserImageable
extend Loginable
has_one :profile, :dependent => :destroy
has_many :items, :dependent => :destroy
has_many :products, :through => :items
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :phone_number, :location, :photo, :profile_attributes, :access_token
delegate :first_name, :last_name, :phone_number, :phone_number=, :location, :location=, :photo, :to => :profile
accepts_nested_attributes_for :profile
end
and
lib/autoloads/loginable.rb
module Loginable
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable
def password_require?
new_record?
end
end
but the server doesn't seem to like that, as it loads with a NoMeethodError
loginable.rb:4:in `<module:Loginable>': undefined method `devise' for Loginable:Module (NoMethodError)
Is there a way to do what I'm shooting for, or not really?
Thanks
This is not the answer you are looking for but, here is my 2 cents: You shouldn't put all that stuff in the User model. devise models have a clear responsibility: signing.
But if you really want to put everything hooked in User.rb, you can split the model in extensions (partially enabling DCI):
Add that to your lib/models/{modelname}/devise_ext.rb
module Models
module User
module DeviseExt
extend ActiveSupport::Concern
included do
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable
end
module ClassMethods
end
module InstanceMethods
def password_require?
new_record?
end
end #InstanceMethods
end
end
end
Then, you just add it into your model:
include Models::User::DeviseExt
In the app we have in my company we actually have no code at all in models, we put everything in extensions.
I've not used Devise yet, but try this:
module Loginable
def self.included(base)
base.send :devise, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable
end
def password_require?
new_record?
end
end
and in your model, include rather than extend:
class User < ActiveRecord::Base
include Loginable
end
This is an old question, but answers here didn't help me with Rails 4.2.
The problem is that when you define instance methods inside a module and include it into User model, they actually get defined on that instance.
But they don't override same methods in devise itself (like email_required?) defined inside devise method. So when you define email_required? on User model itself it works fine, but in included module they don't override devise's method.
But with Ruby 2 you can do this with prepend. Like this:
module User::AuthHelper
extend ActiveSupport::Concern
included do
prepend DeviseInstanceMethods
devise :database_authenticatable, :async, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, \
:omniauthable, omniauth_providers: [:facebook, :instagram], authentication_keys: [:username]
end
module DeviseInstanceMethods
def email_changed?
false
end
def email_required?
false
end
end
end
Now all methods in DeviseInstanceMethods will override devise's methods.
I don't know whether this is best solution, but it works for me. Hope it helps.