I have devise model User
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :meta, polymorphic: true
end
And two types of users that have a polymorphic association to the User model:
class Admin < ActiveRecord::Base
has_one :user, as: :meta, dependent: :destroy
accepts_nested_attributes_for :user
end
class Guest < ActiveRecord::Base
has_one :user, as: :meta, dependent: :destroy
accepts_nested_attributes_for :user
end
Admin's password must have minimum 10 symbols length.
Guest's password must have minimum 6 symbols length.
How I can set different password length for each model, using devise or any different way?
Related
I'm working on rails project, and i want to create some user rating system, it means that each user has the ability to leave feedback on another user, and the user can also change the score he left behind, so the question is how to implement these associations in rails correctly ?
I already have my User model here it is
class User < ApplicationRecord
mount_uploader :avatar, ImageUploader
validates :username, presence: true, uniqueness: true, length: {in: 3..20}
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
enum role: [ :user, :admin ]
end
Well, As I have understood your problem this is ONE-TO-MANY association.
User -many-> Ratings
class User < ApplicationRecord
mount_uploader :avatar, ImageUploader
validates :username, presence: true, uniqueness: true, length: {in: 3..20}
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :ratings, dependent: :destroy
enum role: [ :user, :admin ]
end
Now Create a Rating model which will have its attribute rating_point, user_id and creator_id where user_id is on which rating is given and creator_id is the User who is giving the rating to the user.
class Rating < ApplicationRecord
belongs_to: user
end
Try this It will solve your problem. If not please let me know.
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.
I want to make audits for nested associations like I have
User has_many addresses i.e either
HomeAddress or OfficeAddress
Now if I have just one table Address and I have used type and id for differentiating them.In this case if I use associated_audits for User then it will make just one audit record and whenever I update the record again ,its just replacing the previous audit with the last one.
Here is the models association:
class Patient < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable,
:trackable, :validatable, :confirmable, request_keys: [:subdomain]
has_one :home_address,-> { where(addr_type: 'home') },class_name: 'Address'
has_one :office_address,-> { where(addr_type: 'office') }, class_name: 'Address'
has_associated_audits
accepts_nested_attributes_for :home_address, allow_destroy: true
accepts_nested_attributes_for :office_address, allow_destroy: true
end
class Address < ActiveRecord::Base
belongs_to :patient
audited
end
class Address < ActiveRecord::Base
belongs_to :patient
audited associated_with: :patient
end
class Patient < ActiveRecord::Base
has_many :addresses
has_associated_audits
end
When a user cancels their account on my application their user record is correctly removed from the database but their profile record still seems to exist. Below are my models for user and profile, any solutions?
Profile.rb
class Profile < ActiveRecord::Base
belongs_to :user
end
user.rb
class User < ActiveRecord::Base
has_one :profile
#callback that creates a profile for each user that signs up
after_create :create_profile
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
private
def create_profile
self.profile = Profile.create
end
end
You have to specify explicitly what to do with dependent model.
As example
has_one :profile, dependent: :destroy
There are some other options like :delete, :nullify. You can take a look at them here: http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_one
It will also delete the association too.
has_one :profile, dependent: :destroy
source: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html