After adding the marked line to my Devise user model in a Rails app I can no longer create new users in either the web form or using User.create() on the console. Commenting out this line restores full functionality. Obviously it is very closely related to the line above, it is so that I can have users assigned to a dealership. Any suggestions how to fix or do it differently as I need this functionality?
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :registerable
has_many :tanks, dependent: :destroy
has_many :properties, dependent: :destroy
has_many :contacts, dependent: :destroy
has_many :stations
has_many :clients, :class_name => 'User', :foreign_key => :manager_id
belongs_to :dealer, :class_name => 'User' #<--- this one
end
belongs_to :dealer, :class_name => "User",foreign_key: "dealer_id", optional: true
Note: - make sure you should have dealer_id in User model also to make it working #user.dealer if #user.dealer.present?
Reason
In Rails 5, whenever we define a belongs_to association, it is required to have the associated record present by default.
Related
I am using rails 5.0.0.1
When I submit a form, validations for associated fields are coming into action.
I have Gig, User, Category and other models
I am using devise for user authentication
Gig model
class Gig < ActiveRecord::Base
has_many :proposals
belongs_to :category
has_many :abilities
has_many :skills, through: :abilities
belongs_to :user
end
User model
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 :gigs
has_many :proposals
end
Category model
class Category < ActiveRecord::Base
has_many :gigs
end
When I try to create the gig in console, the transaction rolls back.
the error messages are
["Category must exist", "User must exist"]
I appreciate your help. Thanks in advance.
In rails 5 when you add belongs_to it makes this field required. Try this
belongs_to :user, optional: true
I have three models and here they are when I try to create a has_many. I basically want my users (using devise) to have many categories. And categories to have many users.
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,
:omniauthable
has_many :user_categories
has_many :categories, through: :user_categories
acts_as_messageable
def mailboxer_email(object)
email
end
end
userCategory.rb
class UserCategory < ActiveRecord::Base
belongs_to :user
belongs_to :category
accepts_nested_attributes_for :categories
end
Category.rb
class Category < ActiveRecord::Base
has_many :user_categories
has_many :user, through: :user_categories
validates :name, presence: true, length: {minimum: 3, maximum: 25}
validates_uniqueness_of :name
end
when I run category.users << user I get this error:
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :user_categories in model Category
I can't say for sure what the problem could be, but a few things I could point out:
UserCategory's accepts_nested_attributes_for, does that mean the you want to be able to dynamically create categories?
Category has_many :users, through: :user_categories, not user
You need to follow the Rails file naming conventions, user.rb, user_category.rb and category.rb
These may not be the problem/solution, but I believe they're in the way of resolving the problem.
Let's say I have 3 models in my Rails app... Establishment, WasteType and EstablishmentWaste... My problem is that I want to get all establishments associated with a certain wastetype. This normally would be done with Establishment.where(waste_type_id: some_number), the problem is that Establishment has many WasteType and vice versa, and the association is made through a third party...
Any help?
Classes and data model below
Establishment
class Establishment < ActiveRecord::Base
# Include default devise modules.
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
has_many :containers
has_many :establishment_wastes
has_many :waste_types, through: :establishment_wastes, :foreign_key => 'establishment_id'
include DeviseTokenAuth::Concerns::User
end
WasteType
class WasteType < ActiveRecord::Base
has_many :containers
has_many :establishment_wastes
has_many :establishments, through: :establishment_wastes, :foreign_key => 'waste_type_id'
end
EstablishmentWaste
class EstablishmentWaste < ActiveRecord::Base
belongs_to :establishment, :foreign_key => 'establishment_id'
belongs_to :waste_type, :foreign_key => 'waste_type_id'
end
So the data model would be like these
EstablishmentWaste is a join table in this case so the query should be.
Establishment
.joins(:establishment_wastes)
.where('establishment_wastes.waste_type_id = ?', some_number)
Quick tip! You do not need to assign :foreign_key => 'establishment_id' since it is default Rails behavior to assign a foreign key to model_id.
I am working on a Rails app where a User can create a magazine and another user can subscribe to that magazine. I want to know the best way to do this.
Currently I have a subscription model that builds from current user when created and users the current magazine as the magazine_id. This allow me to have a table of user_ids and magazine_ids. This allows me to see all the subscriptions but it means I cant easily check all the magazines someone subscribes to or check all the subscribers of a magazine.
When I try to use has_many :through, it throws up an error with building from current user. I have put in the relevant code below, hopefully it covers it all and thanks in advance.
User model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :magazines
has_many :subscriptions
end
Subscription model:
class Subscription < ActiveRecord::Base
belongs_to :user
belongs_to :magazine
end
Magazine model:
class Magazine < ActiveRecord::Base
belongs_to :user
has_many :subscrptions
has_many :users
end
The snippet of code from the subscription controller that throws up the error when I use has many through
def new
#subscription = current_user.subscriptions.build
#sub = Sub.find(params[:sub_id])
end
Hopefully that is enough for someone to figure out, if not please ask me for the other code or information.
You're very close, you're just missing the connection. Magazine can see Subscription because Subscription has its magazine_id. User can see Subscription because Subscription has its user_id. Through the Subscription, Magazine and User can see each other. So you want to user through
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :magazines, through: :subscriptions
has_many :subscriptions
end
class Subscription < ActiveRecord::Base
belongs_to :user
belongs_to :magazine
end
class Magazine < ActiveRecord::Base
belongs_to :user
has_many :subscriptions
has_many :users, through: :subscriptions
end
If this doesn't work, make sure you post your schema.rb/relevant fields from the tables you mentioned.
I think this should work, might need to set additional options though.
User model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :magazines
has_many :subscriptions
has_many :subscribed_magazines, through: :subscriptions, source: :magazine
end
Subscription model:
class Subscription < ActiveRecord::Base
belongs_to :user
belongs_to :magazine
end
Magazine model:
class Magazine < ActiveRecord::Base
belongs_to :user
has_many :subscriptions
has_many :subscribed_users, through: :subscriptions, source: :user
end
Edit: Needed source, not class_name
Here is my join model:
class CompanyUser < ActiveRecord::Base
belongs_to :company
belongs_to :user
end
My User model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
ROLES = %w[admin requestor requestor_limited shipping_vendor].freeze
attr_accessor :temp_password
has_many :companies_users
...
end
If I run this in the console:
u = User.first
u.companies
This is the error I am getting:
NameError: uninitialized constant User::CompaniesUser
has_many through relationships should be like this:
In app/models/company.rb file,
has_many :company_users
has_many :users, :through => :company_users
In app/models/user.rb file,
has_many :company_users
has_many :companies, :through => :company_users
In app/models/company_user.rb file,
belongs_to :company
belongs_to :user
If you want to delete the dependent records in company_users table when deleting companies/users,
Add, , :dependent => :destroy at the end of has_many relations in Company and User model.
Hope this helps you..
Thanks.!!
it must be
has_many :company_users
"CompanyUser".tableize => "company_users"
The model shall be either:
class CompaniesUser < ActiveRecord::Base
belongs_to :company
belongs_to :user
end
Or has_many declaration sheel be defined explicitly as:
class User < ActiveRecord::Base
has_many :company_users
end