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.
Related
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 have the following situation:
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
after_create :create_profile
after_create :programstart
has_one :profile, dependent: :destroy
has_many :weights, dependent: :destroy
has_many :programstarts, dependent: :destroy
has_many :user_nutrients, dependent: :destroy
has_many :nutrients, through: :user_nutrients, dependent: :destroy
private
def programstart
Programstart.create(:user_id => id)
end
end
nutrient.rb
class Nutrient < ActiveRecord::Base
validates :name, uniqueness: true
has_many :user_nutrients
has_many :users, through: :user_nutrients
end
user_nutrient.rb
class UserNutrient < ActiveRecord::Base
belongs_to :user
belongs_to :nutrient
end
For profile, weights and programstarts the dependent: :destroy works. All the associated database entries are removed when I delete the user. However, for user_nutrients the dependent: :destroy is not working. After deleting the user these entries are stil there.
What am I missing here?
According to this post -
http://makandracards.com/makandra/32175-don-t-forget-automatically-remove-join-records-on-has_many-through-associations
you should do the following:
has_many :user_nutrients
has_many :nutrients, through: :user_nutrients, dependent: :destroy
and in nutrient.rb
has_many :user_nutrients
has_many :users, through: :user_nutrients, dependent: :destroy
I am writing a function which, on update of any attribute of a model, sets the status of is_kyc_verified to false.
Here is the code of the User model and the method which changes the status:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
before_update :change_kyc_status, unless: :is_kyc_verified_changed?
#Associations
has_one :address, dependent: :destroy
has_one :kyc, dependent: :destroy
has_one :pan_detail, dependent: :destroy
has_one :document, dependent: :destroy
has_many :nominee_details, dependent: :destroy
has_many :bank_details, dependent: :destroy
#Accept Attributes for associated model
accepts_nested_attributes_for :address, :kyc, :pan_detail,
:document, :nominee_details, :bank_details,
allow_destroy: true, reject_if: :all_blank
#validates
validates :name, :mobile_no, :gender, :dob, presence: true
validates :mobile_no, numericality: true, length: { is: 10 }
private
##
# Check if is_kyc_verified is set to true
# if 'yes' then alert user and set is_kyc_verified to false
def change_kyc_status
self.is_kyc_verified = false if self.valid? and self.is_kyc_verified.present?
true
end
end
The method initially used to return the self.is_kyc_verified, which was false, in turn resulted in "rollback of transaction". I explicitly added a true at the end so it won't rollback the transaction.
However I feel that this is not the right way to implement this function.
Can you please review my code and suggest the right way to do so?
What is the underlying purpose of is_kyc_verified? Is there a reason you actually need to return true or false? It seems like return alone would do the trick:
def change_kyc_status
is_kyc_verified = (valid? && is_kyc_verified.present?) ? false : true
return
end
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?
I have these models:
class Item < ActiveRecord::Base
has_many :item_categoryships
has_many :categories, class_name: 'ItemCategoryship', foreign_key: 'category_id', :through => :item_categoryships
belongs_to :user
validates :title, presence: true
end
class Category < ActiveRecord::Base
has_many :item_categoryships
has_many :items, :through => :item_categoryships
belongs_to :user
validates :name, presence: true
end
class ItemCategoryship < ActiveRecord::Base
belongs_to :item
belongs_to :category
validates :item_id, presence: true
validates :category_id, presence: true
end
class User < ActiveRecord::Base
has_many :items
has_many :categories, class_name: 'Category'
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :async
end
But I found when I call item.categories will get a empty array !!! I have checked database, there is a record here.
When I test in the rails console, I didn't get any record back, just saw 'ActiveRecord::Associations::CollectionProxy []'.
What is this? I am using Rails 4.0.2.
Thanks you all.
ActiveRecord::Associations::CollectionProxy is ActiveRecord class for collection associations. Now, your code should work if you change line in Item class describing categories association to:
has_many :categories, through: :item_categoryships