Undefined method `each' for #<User: - ruby-on-rails

Rails 4.1
Ruby 2.0
Credential.rb
class Credential < ActiveRecord::Base
belongs_to :category
has_many :user
validates :name, :login, :password, presence: true
attr_accessor :encryption_key
attr_encrypted :login, key: :encryption_key
attr_encrypted :password, key: :encryption_key
end
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
has_many :credentials
def you
"You are <b>#{email}</b>"
end
end
CredentialsController.rb
class CredentialsController < ApplicationController
before_filter :authenticate_user!
def create
#credential = current_user.credentials.new
#credential.encryption_key = session[:master_key]
#credential.update(credential_params)
if #credential.save
redirect_to credential_path(#credential), notice: "Password entry created successfully."
else
render "new"
end
end
The line:
#credential.update(credential_params)
throws an exception
undefined method 'each' for #<User:0x4de4f58>

You need to edit your associations. You have credentials that has_many :user and users that has_many :credentials. The one with the foreign key should be a belongs_to not has_many. If you're attempting to make a many-to-many relationship, then either use has_many_and_belongs_to or a join table. Further, it should be has_many :users and not has_many :user. That should resolve your error.

Related

Cannot create new record with self Joins relationship User Table in Rails and Devise

I got a problem with my rails app. Here is my code in my Model:
class User < ApplicationRecord
has_many :assistants, class_name: User, foreign_key: :manager_id
belongs_to :manager, class_name: User
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
#validation
validates :first_name, :middle_name, :last_name, :birth_date, presence: true
end
Here is my Migration:
class AddReferenceToUserTable < ActiveRecord::Migration[5.0]
def change
add_column :users, :manager_id, :integer
end
end
BUT here is the error in my VIEW:
Sign up
1 error prohibited this user from being saved:
Manager must exist
May I know what wrong with my code?
Thanks,
Randz
If this is a rails 5 application belongs to is by default required. If a user will not always have a manager then you want
belongs_to :manager, class_name: User, required: false

Devise Model does not allow Association

I recently downloaded and installed Devise for Rails 4. I used it on the model "actors"; which have a subset model called "employee"
For the class "Actor"
class Actor < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
include UUIDHelper
has_one :access, autosave: true
has_one :employee, autosave: true
has_many :contact_detail, autosave: true
has_many :file_set
has_many :link_set
mount_uploader :logo, AvatarUploader
validates :name, uniqueness: true
validates_presence_of :name
validates_length_of :description, maximum: 256
def actor_name
actor.name
end
validates :logo,
:file_size => {
:maximum => 25.megabytes.to_i
}
end
For the class "Employee"
class Employee < ActiveRecord::Base
include UUIDHelper
belongs_to :actor
has_one :status
has_many :restdays
has_one :regular_work_period
validates_presence_of :actor
end
I previously had this relation unto a view:
<td><%= employee.actor.name %></td>
Now it spews out an error:
undefined method `name' for nil:NilClass
What did I do wrong? Did Devise do something?
That error indicates this employee object doesn't have actor. employee.actor outputs nil.
Just check your employee object is loaded correctly. It is not a Devise problem.

accepts_nested_attributes with 2 nested tables

I am new to Ruby on rails and I am creating simple website. I found out the rails 3 uses attr_accessible while rails 4 uses strong parameters. I am currently working with rails 4. I am not quite sure how to set up the accepts_nested_attributes_for.
This is what I have
User model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:rememberable, :validatable
has_many :expense_pictures
has_many :income_pictures
accepts_nested_attributes_for: ExpensePicture ?
accepts_nested_attributes_for: IncomePicture ?
end
ExpensePicture model:
class ExpensePicture < ActiveRecord::Base
belongs_to :user
mount_uploader :image, ImageUploader
end
ExpenseText model:
class ExpenseText < ActiveRecord::Base
belongs_to :expense_pictures
end
IncomePicture model:
class IncomePicture < ActiveRecord::Base
belongs_to :user
mount_uploader :image, ImageUploader
end
IncomeText model:
class IncomeText < ActiveRecord::Base
belongs_to :income_pictures
end
My User controller
class UserController < ApplicationController
def create
User.create(user_params)
accepts_nested_attributes_for :IncomePicture ?
accepts_nested_attributes_for :ExpensePicture ?
end
private
def user_params
# required input for params
# permit - returns a version of the params hash with ony the permitted attributes
params.require(:user).permit(:name, :email, :password, :password_confirmation, ...not sure...)
end
end
Using accepts_nested_attributes_for for has_many relation with your models,your User model should look like this
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:rememberable, :validatable
has_many :expense_pictures
has_many :income_pictures
accepts_nested_attributes_for :expense_pictures
accepts_nested_attributes_for :income_pictures
end
Your new and create methods of usercontroller should look like this
def new
#user = User.new
#user.expense_pictures.build
#user.income_pictures.build
end
def create
#user = User.new(user_params)
if #user.save
redirect_to #user
else
render 'new'
end
end
And your user_params method should look something like this
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, expense_pictures_attributes: [:your_attr1,:your_attr2,..],income_pictures_attributes: [:your_attr1,:your_attr2,..])
end
Feel free to ask if you want any more information regarding to this.

How to establish association between a Devise model and a model I have made?

How do you use User(devise which store email and password) to belong_to Profile and Profile has_one User? When I looked up the database profile_int is still nil hmmm... not sure where did I do wrong?
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_id
# attr_accessible :title, :body
belongs_to :profile
end
class Profile < ActiveRecord::Base
attr_accessible :dob, :firstName, :lastName, :school_id, :schYear, :user_attributes
belongs_to :school
has_one :user
accepts_nested_attributes_for :user
end
I know usually I should do something like this Profile.create(.......) but I am not sure where to do this if I am doing it with a devise
Should :schoolstrong text be ':schoolstrong_text'? Did you run rake db:migrate ?
Also, checking your schema could be helpful too.
A common practice in associating a Devise User with a Profile model is making the profile the child of the user:
# app/models/user.rb
class User < ActiveRecord::Base
has_one :profile
end
# app/models/profile.rb
class Profile < ActiveRecord::Base
belongs_to :user
end
Then, within your User model, you'd create an after_create hook to create a new Profile model and associate it with the newly created user:
# app/models/user.rb
after_create :create_profile
def create_profile
profile = Profile.create
self.profile = profile
self.save
end

Rails Devise polymorphic

I am using devise and I want create a polymorphic relationship, I added the columns to table users 'usersable_type' and 'usersable_id'
This is my code
Model >> User
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
# attr_accessible :title, :body
attr_accessible :email, :password, :password_confirmation, :remember_me
#usarsable
belongs_to :usersable, :polymorphic => true, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :usersable
end
Model >> Medic
class Medic < ActiveRecord::Base
attr_accessible :license_number, :specialty
has_one :user, as: :usersable, dependent: :destroy
has_and_belongs_to_many :patients
end
Model >> Patient
class Patient < ActiveRecord::Base
belongs_to :socialsecurity
attr_accessible :birthday, :blood_type
has_one :user, as: :usersable, dependent: :destroy
has_many :contacts
has_and_belongs_to_many :medics
end
Override Devise Controller
class RegistrationsController < Devise::RegistrationsController
def new
super
#user.build_usersable # I had problem in this line
end
def create
end
def update
super
end
end
Those are all the models I have for the moment, but I still have the same problem, I dont know how create and save the polymorphic object.
The error still is the same
ERROR: undefined method `build_usersable' for "<#User:"
Is anyone can help me I would be grateful
Regards and thanks in advance
Juli.
Based on the conversation in the comments this is the kind of scheme i think you need.
I am going to use the concept of Aspect, that is a user can have many aspects, the aspect might be a medic, a content detail etc; an Aspect can have only one user.
First you need a UserAspect model which has a user_id and a aspect_type and aspect_id
class UserAspect < ActiveRecord::Base
belongs_to :user
belongs_to :aspect, :polymorphic => true
end
Now your user model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
# attr_accessible :title, :body
attr_accessible :email, :password, :password_confirmation, :remember_me
#aspects
has_many :user_aspects
has_many :aspects, :through => :user_aspects
end
And now your medic
class Medic < ActiveRecord::Base
attr_accessible :license_number, :specialty
has_one :user_aspect, as: :aspect, dependent: :destroy
has_one :user, :through => :user_aspect
end
Now you can do things like
user.aspects
medic.user
etc

Resources