ActiveModel::UnknownAttributeError - Uknown Attribute - ruby-on-rails

I am trying to create a history from other actions inside my User model through BookingHistory model.
Like this:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :rooms
has_many :booking_histories, foreign_key: :booking_history_id
accepts_nested_attributes_for :booking_histories
def set_history(room)
history = self.booking_histories.build
history.booking = room.booking
history.room = room
history.total_price = room.booking.order
history.time_spent = room.booking.time_at_room
history.charged = false
end
end
But I'm getting this error and I don't know why. I've already made the one-to-many relationship.
THe error
Does anyone have any idea of what's going on?

Related

Cannot create record for associated model in Rails 5

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.

Unable to autoload constant JWTBlacklist, expected /home/sourabh/dev/celebration/app/models/jwt_blacklist.rb to define it (LoadError)

my user.rb model contains:
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :lockable, :timeoutable, :omniauthable,
:jwt_authenticatable, jwt_revocation_strategy: JWTBlacklist
def send_devise_notification(notification, *args)
devise_mailer.send(notification, self, *args).deliver_later
end
end
I am using devise-jwt gem to signin for my rails api.
my JWTBlacklist.rb model contains:
class JwtBlacklist < ApplicationRecord
include Devise::JWT::RevocationStrategies::Blacklist
self.table_name = 'jwt_blacklist'
end
Your User class is looking for JWTBlacklist, but your file is defining JwtBlacklist. You need to change one of those to match the other.

Error shown when generating models in RoR, wrong number of arguments in model

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

How can i get separate tables for manager , owner , visitor as i am using rolify gem?

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

Sass::SyntaxError in Devise::Registrations#new

I for the first time use Devise. When generated the user model and I tried to register the first user according to the link - http://localhost:3000/users/sign_up. There is a mistake: http://i.stack.imgur.com/44uMT.png
routes.rb:
Diary::Application.routes.draw do
devise_for :users
root 'static_pages#welcome', as: 'welcome'
end
user.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Thanks!

Resources