I want to do STI in Rails.
class AbstractUser < ActiveRecord::Base
self.table_name = 'users'
belongs_to :organization, :inverse_of => :users
# reporter user
has_many :requests, :dependent => :destroy
# startup user
has_many :responses, :dependent => :destroy
has_many :startup_requests, :through => :responses, :source => :request
scope :reporters, where(:type => 'Reporter')
scope :startup_employees, where(:type => 'Startup')
scope :on_waitlist, where(:waitlist => true)
scope :not_on_waitlist, where(:waitlist => false)
end
require 'rfc822'
class User < AbstractUser
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :confirmable
validates :name, :presence => true
validates :surname, :presence => true
validates :title, :presence => true
validates :password, :presence => true, :length => { :minimum => 8 }
validates :email, :presence => true, :format => { :with => RFC822::EMAIL_REGEXP_WHOLE }
attr_accessible :name, :surname, :title, :organization,
:email, :password, :fullname
end
require 'rfc822'
class UserForAdmin < AbstractUser
validates :email, :presence => true, :format => { :with => RFC822::EMAIL_REGEXP_WHOLE }
validates :organization_id, :presence => true
attr_accessible :name, :surname, :title, :organization, :email,
:password, :fullname, :password_confirmation, :type,
:organization_id, :waitlist, :invitation_token
end
And there is some problem with these scopes.
Couldn't find UserForAdmin with id=7 [WHERE "users"."type" IN ('UserForAdmin') AND "users"."waitlist" = 'f']
I also tried to put these scopes in UserForAdmin instead of AbstractUser with the same result. I (probably) need scopes instead of custom methods, because I use them in ActiveAdmin. How can I solve this?
If you don't want to receive all users, you need to query with the base class. In a simpler example:
class Animal < ActiveRecord::Base
end
class Dog < Animal
end
class Cat < Animal
end
Dog.create
Cat.create
Animal.all
=> [dog, cat]
Dog.all
=> [dog]
Cat.all
=> [cat]
So, in your case, you'd want to:
AbstractUser.not_on_waitlist.find(params[:id])
If this user is a UserForAdmin you'll receive an object of class UserForAdmin. If it's just a user, you'll receive an object of class User
Related
When updating car info, the validation process fails because I have validates_uniqueness_of :number
class Car < ActiveRecord::Base
validates :number,numericality: true, length: {is: 7 }
validates :number, :name, presence:true
validates_uniqueness_of :number, :message => "מספר רכב זה קיים במערבת"
belongs_to :owner
has_many :visits
end
I need validation to pass, if the original value was not changed, validation on_create would not help since I still need validation when updating.
Any help would be really appreciated.
This will work for you :
validates :number, :uniqueness => {:scope => :number}, :message => "מספר רכב זה קיים במערבת"
OR
validates_uniqueness_of :number, :message => "מספר רכב זה קיים במערבת", :scope => :number
I want to create a page view that shows a leaderboard of an user. That user can have many friends and those friends can have many activities. How can I make a query that returns the top 10 users with most activities done? Example: there are 50 users in the system and i want to see returned "User 1 : 53 activities ";"User 2 : 49 activities " , ... User 10 : 22 activities ".
class Account < ActiveRecord::Base
has_many :friendships
has_many :passive_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
has_many :active_friends, -> { where(friendships: { approved: true}) }, :through => :friendships, :source => :friend
has_many :passive_friends, -> { where(friendships: { approved: true}) }, :through => :passive_friendships, :source => :account
has_many :pending_friends, -> { where(friendships: { approved: false}) }, :through => :friendships, :source => :friend
has_many :requested_friendships, -> { where(friendships: { approved: false}) }, :through => :passive_friendships, :source => :account
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :activities
has_many :sports, through: :activities
GENDER_TYPES = ["Male", "Female"]
validates :name, presence: true, length: {maximum: 30}
validates :gender, presence: true
validates :year_Of_Birth, presence: true
validates :month_Of_Birth, presence: true
validates :day_Of_Birth, presence: true
validates :country, presence: true
def friends
active_friends | passive_friends
end
class Friendship < ActiveRecord::Base
belongs_to :account
belongs_to :friend, :class_name => "Account"
end
class Activity < ActiveRecord::Base
has_one :sport
belongs_to :account
has_one :place
AMBIENT_TYPES = ["Outdoor", "Indoor"]
validates :name, presence: true
validates_numericality_of :duration, :on => :create, presence: true
validates :year, presence: true
validates :month, presence: true
validates :day, presence: true
validates :resume, length: {maximum: 200}
And where should I write the query? In views i created a directory called pages and made a html.erb file called leaderboard inside it. I have a controller named pages. I should make a function in there? like :
def leaderboard
"and put the query here right?"
end
It looks like you can add a counter to the Activity class:
class Activity < ActiveRecord::Base
has_one :sport
belongs_to :account, :counter_cache => true
has_one :place
...
then add a column to your Account table called activities_count where you can then call:
Account.find(:all, :order => 'activities_count', :limit => 10)
I used this answer to help (there is also another method listed here): similar questions
Following is my models:
class Poll < ActiveRecord::Base
attr_accessible :published, :title
validates :published, :presence => true
validates :title, :presence => true,
:length => { :minimum => 10 }
has_many :choice, :dependent => :destroy
end
class Choice < ActiveRecord::Base
belongs_to :poll
attr_accessible :choice_text, :votes
validates :choice_text, :presence => true
end
I then tried to install the rails admin. I was able to create the choices and polls in the admin, but i was unable to associate a choice with a poll and vice versa.
How can i do it?
First of all in has_many class name should be in plural:
has_many :choices
And you should add attr_accessible poll_id or choice_ids for Model from which you want to edit this association. Or just delete all attr_accessible for first try.
class Poll < ActiveRecord::Base
attr_accessible :published, :title, choice_ids
validates :published, :presence => true
validates :title, :presence => true, :length => { :minimum => 10 }
has_many :choices, :dependent => :destroy
end
class Choice < ActiveRecord::Base
belongs_to :poll
attr_accessible :choice_text, :votes, :poll_id
validates :choice_text, :presence => true
end
There is no attr_accessible in Rails 4. Use accepts_nested_attributes_for instead. More info:
https://github.com/sferik/rails_admin/wiki/Belongs-to-association
Model Code (Attempting to require uniqueness for embed_code)
https://gist.github.com/1427851:
class Link < ActiveRecord::Base
validates :embed_code,
:format => {:with => /^<object type|^<embed src|^<object width|^<iframe src|^<object height|^<iframe width|^<embed id|^<embed width|^<object data|^<div|^<object id/i, :message => "Invalid Input"},
:uniqueness => true
attr_accessible :title, :embed_code, :score
after_initialize :calculate_score, :favs_count
attr_accessor :score, :fav_count
validates :title, :length => { :in => 4..45, :message => "Must be between 4 & 45 characters"}
before_save :resize
has_many :favorites
has_many :favorited, :through => :favorites, :source => :user
belongs_to :user
I've tried validates_uniqueness_of :embed_code and disabling (commenting out) non-critical components of the model such as :before_save :resize
I am trying to write a unit testing for a User model in Ruby on Rails. I am using authlogic and need to check that the first_name and last_name of the user model attributes are not the same when the user is registering.
This is my user model:
class User < ActiveRecord::Base
acts_as_authentic do |c|
c.login_field= :username
end
has_many :memberships, :class_name => "Project::Membership"
has_many :projects, :through => :memberships
has_one :profile
validates :email, :presence => true, :uniqueness => true
validates :username, :presence => true, :uniqueness => true
validates :first_name,:presence => true
validates:last_name, :presence => true
validates :title, :presence => true
validates :password, :presence => true
validates :password_confirmation, :presence => true
validates :gender, :presence => true
# Custom validator
validates :first_name, :last_name, :different_names => true
As you can see, I tried to create a custom validator creating a new file in /lib/different_names_validator.rb with a class called DifferntNamesValidator, but couldn't get it, as I got the following error: Unknown validator: 'different_names' (ArgumentError)
Thanks in advance!
Hi Try to include this module in your model