this is my user model:
class User < Account
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
devise :database_authenticatable,
:ga_otp_authenticatable,
:yk_otp_authenticatable,
:registerable,
:confirmable,
:recoverable,
:trackable,
:validatable,
:lockable,
:timeoutable
# Setup accessible (or protected) attributes for your model
attr_accessible :password, :password_confirmation, :remember_me, :time_zone, :require_ga_otp, :require_yk_otp, :full_name, :address
attr_accessor :captcha,
:skip_captcha,
:new_password,
:new_password_confirmation,
:current_password
before_validation :generate_name,
:on => :create
has_many :trade_orders,
:dependent => :destroy
has_many :purchase_trades,
:class_name => "Trade",
:foreign_key => "buyer_id"
has_many :sale_trades,
:class_name => "Trade",
:foreign_key => "seller_id"
has_many :invoices,
:dependent => :destroy
has_many :yubikeys,
:dependent => :destroy
has_many :bank_accounts,
:dependent => :destroy
has_many :tickets,
:dependent => :destroy
#has_many :tickets,
# :dependent => :destroy
validates :email,
:uniqueness => true,
:presence => true
validate :captcha do
if captcha.nil? and new_record?
unless skip_captcha
errors[:captcha] << I18n.t("errors.answer_incorrect")
end
end
end
def captcha_checked!
self.captcha = true
end
def bitcoin_address
super or (generate_new_address && super)
end
def qr_code
if #qrcode.nil?
#qrcode = RQRCode::QRCode.new(bitcoin_address, :size => 6)
end
#qrcode
end
def confirm!
super
UserMailer.registration_confirmation(self).deliver
end
protected
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
name = conditions.delete(:name)
where(conditions).where(["name = :value OR email = :value", { :value => name }]).first
end
def generate_name
self.name = "BF-U#{"%06d" % (rand * 10 ** 6).to_i}"
end
end
This is my account model:
class Account < ActiveRecord::Base
has_many :account_operations
has_many :used_currencies,
:dependent => :destroy
belongs_to :parent,
:class_name => 'Account'
validates :name,
:presence => true,
:uniqueness => true
# BigDecimal returned here
def balance(currency, options = {} )
account_operations.with_currency(currency).with_confirmations(options[:unconfirmed]).with_processed_active_deposits_only.map(&:amount).sum.round(5).abs
end
# Generates a new receiving address if it hasn't already been refreshed during the last hour
def generate_new_address
unless last_address_refresh && last_address_refresh > DateTime.now.advance(:hours => -1)
self.last_address_refresh = DateTime.now
self.bitcoin_address = Bitcoin::Client.instance.get_new_address(id.to_s)
save
end
end
def max_withdraw_for(currency)
Transfer.round_amount(self.balance(currency), currency)
end
def self.storage_account_for(currency)
account_name = "storage_for_#{currency.to_s.downcase}"
account = find_by_name(account_name)
if account
account
else
Account.create! do |a|
a.name = account_name
end
end
end
end
My problem is I am trying to input the password but it is not accepting password field. It is giving error that password cant be blank.
I have tried to create user manually. here it is giving error undefined method encrypted password.
how to solve this problem?
Try out this.....
Do you have the encrypted_password column on your model?
Yes
attr_accessible :email, :password, :password_confirmation, :remember_me, :encrypted_password
Just because it's attr_accessable doesn't mean the attribute exists, just means you can access it (if it did exist). Go into your rails console and run:
User.new.respond_to?(:encrypted_password=)
If that returns true you have the column in your model, if not you need to make sure you run the correct migrations.
User.new.respond_to?(:encrypted_password=) => false
I run rake db:migrate:reset and it work!!!
Related
I'm trying to dynamically build a collection where each array contains a value from two separate tables.
The Models:
#/models/user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :user_id, :email, :password, :password_confirmation, :remember_me,
:first_name, :last_name, :permanent_address, :permanent_city,
:permanent_state, :permanent_zip, :home_phone, :mobile_phone, :role,
:tenant_attributes, :rents_attributes
validates :email, :presence => true, :uniqueness => true
validates :first_name, :presence => true
validates :last_name, :presence => true
validates :permanent_address, :presence => true
validates :permanent_city, :presence => true
validates :permanent_zip, :presence => true
validates :first_name, :presence => true
validates :home_phone, :presence => true
has_one :app
has_one :tenant, :foreign_key => :users_id
has_many :rents
has_many :maints
accepts_nested_attributes_for :tenant
accepts_nested_attributes_for :rents, allow_destroy: true
end
#/models/tenant.rb
class Tenant < ActiveRecord::Base
belongs_to :users
belongs_to :units
attr_accessible :lease_begin, :lease_end, :rent_share, :users_id, :units_id
has_many :maints
end
The Helper Method (so far):
#/helpers/users_helper.rb
def tenants
tenants = Tenant.select([:id, ??? ])
tenants.map {|u| [u.???, u.id]}
end
The form field:
<%= f.input :tenant_id, :collection => tenants %>
Essentially what I'm trying to do is select the :id from the Tenants table and then the associated :first_name + :last_name (represented by "???" above) from the Users table to populate the collection arrays this would generate.
What's the best approach here?
If your helper is specifically used just for this input then I believe you have the correct thought on your query, in that you are concerned on retrieving just the required columns.
To retrieve tenants including attributes from their belongs_to :user relation, your helper definition needs to be updated to:
# app/helpers/users_helper.rb
def tenants
tenants = Tenant.joins(:user).select('tenants.id as tenant_id, users.first_name, users.last_name')
tenants.map { |u| [ [u.first_name, u.last_name].join(' '), u.tenant_id ]}
end
The tenants helper then returns a nested array of first_name and last_name joined with a space as one element and tenant_id as the second element array.
With the updated helper your view would be:
<%= f.select :tenant_id, :collection => tenants %>
Note the use of select helper here which is more suitable for this case.
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
I've four user models: Zone, Product, User, Group
I want to choose what Users can sell a Product in a Zone, this is what Group does, with a many to many relation to User and a foreign key to one Product and one Zone. So I have one group per pair Zone/Product. I will also need to set custom attributes on that many to many relation so I used has_many :through Sell (I was unable to find a better name to describe the relation between Group and User).
So I ended up having 5 models: Zone, Product, User, Group, Sell.
It works fine, but now I'd need to select the next user available in a Group.
I was thinking to exploit Sell.id to find the user assigned to the same group with an higher id, if not present choose the first one again (this allows me to create a ring chain).
It would be useful to have a Group.next_user method.
Unfortunatly I can't figure out how to do this, I'd need help to find the next user available in the group (or the 1st one if there are no more users).
Follows the code for models all the models:
################
# models/group.rb
################
class Group < ActiveRecord::Base
has_many :sells
has_many :users, :through => :sells
belongs_to :zone
belongs_to :product
attr_accessible :priority, :product_id, :user_ids, :zone_id
end
################
# models/zone.rb
################
class Zone < ActiveRecord::Base
belongs_to :location
has_many :cities
has_many :groups
attr_accessible :name, :location_id
validates :location, :presence => true
end
################
# models/user.rb
################
class User < ActiveRecord::Base
after_create :create_calendar
before_destroy :destroy_calendar
belongs_to :location
belongs_to :mall
has_one :event_calendar
has_many :sells
has_many :groups, :through => :sells
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable, :registerable,
# :recoverable, :rememberable,
devise :database_authenticatable, :trackable,
:validatable, :authentication_keys => [:username]
# Setup accessible (or protected) attributes for your model
attr_accessible :username, :password, :password_confirmation, :remember_me, :name,
:surname, :role, :location_id
# attr_accessible :title, :body
ROLES = %w[Admin Agente Hostess HostessAdmin]
validates_uniqueness_of :username, :case_sensitive => false
validates :username, :presence => true
validates_presence_of :role, :name, :surname, :location_id
validates :location, :presence => true
validates :role, :inclusion => { :in => ROLES, :message => "%{value} non è un ruolo valido." }
def display_name
"#{self.name} #{self.surname}"
end
def has_role?(role)
# convert the role string to a sybmol
self.role.downcase.gsub(/\s+/, "_").to_sym == role
end
private
def create_calendar
if self.has_role? :agente
calendar = EventCalendar.new({:user_id => self.id})
calendar.save()
end
end
def destroy_calendar
if self.has_role? :agente
calendar = EventCalendar.find_by_user_id(self.id)
calendar.destroy()
end
end
def email_required?
false
end
def email_changed?
false
end
end
################
# models/product.rb
################
class Product < ActiveRecord::Base
after_create :create_groups
before_destroy :destroy_groups
attr_accessible :name
def create_groups
for zone in Zone.all
group = Group.new({:zone_id => zone.id, :product_id => self.id})
group.save()
end
end
def destroy_groups
for zone in Zone.all
group = Group.find_by_product_id(self.id)
group.destroy
end
end
end
################
# models/sell.rb
################
class Sell < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
Can you give me some help to get this done? Thanks!
If I get this right then addd this to your User model
scope :next, lambda { |p| {:conditions => ["id > ?", p.id], :limit => 1, :order => "id"} }
and this to your group model
def self.next_user
return User.first if Group.users.blank?
next_user = User.next(Group.users.last).first
return next_user || Group.users.first
end
This should do the trick. I didn't write test for this so you should test it :)
I am trying to update a user record using a formtastic nested form. Its structure is as ollows
User
Admin
Address
When I send the form to update details, while updating the address or admin record, the user_id(foreign key) gets set to NULL. This is the data that gets sent and it seems to be ok.
Parameters: {
"utf8"=>"✓", "authenticity_token"=>"some token",
"user"=>{
"id"=>"16",
"first_name"=>"User",
"last_name"=>"Name",
"email"=>"username#gmail.com",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"address_attributes"=>{
"main_phone"=>"131231233",
"address1"=>"Address 1 Line",
"address2"=>"Address 2 Line",
"city"=>"Lansing",
"state"=>"Michigan",
"zip"=>"48823",
"user_id"=>"16"
},
"admin_attributes"=>{
"company_id"=>"2",
"user_id"=>"16"
},
"roles_mask"=>"1",
"user_id"=>"16"
},
"commit"=>"Update User Roles",
"company_id"=>"2",
"id"=>"16"
}
User Model
class User < ActiveRecord::Base
has_one :address, :dependent => :destroy, :inverse_of => :user
has_one :admin, :dependent => :destroy, :inverse_of => :user
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :roles_mask, :terms_of_use,:id
attr_accessible :owner_attributes, :admin_attributes, :address_attributes, :client_attributes
accepts_nested_attributes_for :owner, :admin, :client, :address
end
Admin Model
class Admin < ActiveRecord::Base
belongs_to :company
belongs_to :user, :inverse_of => :admin
attr_accessible :company_id, :user_id
end
*Address Model
class Address < ActiveRecord::Base
belongs_to :user, :inverse_of => :address
attr_accessible :address1, :user_id, :address2, :city, :state, :zip, :main_phone, :cell_phone
end
Could you please help me with this. Thanks.
UPDATED with the model details. I removed the validation to keep it short.
Try to correct
accepts_nested_attributes_for :owner, :admin, :client, :address
in your User model with
accepts_nested_attributes_for :owner, :client
accepts_nested_attributes_for :admin, :address, :update_only => true
I'm having issues displaying values from a nested controller. I can update the field just fine, I just cannot see the values on the show page. I think it's an issue with Devise.
My user model is as follows:
class User < ActiveRecord::Base
has_one :page
accepts_nested_attributes_for :page, :allow_destroy => true
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :name
has_friendly_id :name, :use_slug => true, :strip_non_ascii => true
validates_presence_of :name
validates_uniqueness_of :name
validates_uniqueness_of :email, :case_sensitive => false
#creates a new page on user create if one doesn't exist
def after_initialize
self.build_page if self.page.nil?
end
end
My pages model:
class Page < ActiveRecord::Base
belongs_to :user
attr_accessible :tagline, :about, :email, :phone, :website, :blog, :user,
end
routes.rb
Ahoy::Application.routes.draw do
resources :users, :path => '/' do
resource :page
end
devise_for :users, :controllers => { :registrations => "registrations"}
get "home/index"
get "home/about"
root :to => "home#index"
end
In my users#show I have this:
<p>
<strong>Tagline</strong>
<%= #user.tagline %>
</p>
And I get a undefined method. Have also tried, #pages.tagline etc.
I didn't think I needed to amend my controller? Can you help?
The tagline method is defined in Page, so it should be #user.page.tagline