Mass Assignment confusion in rails association - ruby-on-rails

Having some trouble with something I'm sure is basic, but also having trouble finding a good answer here on SO.
I have a Users table and an Authorization table, here are my models:
##Authorization.rb
class Authorization < ActiveRecord::Base
attr_accessible :provider, :uid, :user_id
belongs_to :user
validates :provider, :uid, :presence => true
def self.find(auth_hash)
find_by_provider_and_uid(auth_hash["provider"],
auth_hash["uid"])
end
def self.create_with_hash(auth_hash)
#if they've already registered, then just return that authorization
unless auth = find_by_provider_and_uid(auth_hash["provider"],
auth_hash["uid"])
user = User.create(name: auth_hash["info"]["name"],
email: auth_hash["info"]["email"],
nickname: nil,
firstname: auth_hash["info"]["first_name"],
location: auth_hash["user_location"]
)
auth = create(user: user,
provider: auth_hash["provider"],
uid: auth_hash["uid"])
end
auth
end
end
And my User model:
##User.rb
require 'bcrypt'
class User < ActiveRecord::Base
include BCrypt
#accessible and settable properties
attr_accessible :name, :email, :nickname, :firstname, :location
#relations
has_many :authorizations, dependent: :destroy
#validations
validates :name, :email, :firstname, :presence => true
validates :email, :uniqueness => true
validates :nickname, :uniqueness => true
#always make sure their email and nickname are lowercased
def before_validation(user)
user.email.downcase!
user.email = Password.create(email)
user.nickname.downcase!
end
def after_save(user)
user.email = Password.new(user.email)
end
def is_nickname_available?(nickname)
Users.find_by_nickname(nickname.downcase).blank?
end
def add_nickname(user_id, nickname)
#todo: error handling
user = Users.find(user_id).update_attribute(nickname: nickname)
end
def add_provider(auth_hash)
#Check if the provider already exists, so we don't add it twice
unless
authorizations.find_by_provider_and_uid(auth_hash["provider"],
auth_hash["uid"])
Authorization.create user_id:self.id,
provider: auth_hash["provider"],
uid: auth_hash["uid"]
end
end
end
In a controller, if I try to do: Authorization.create_with_hash(auth_hash), I get the error:
Can't mass-assign protected attributes: user
On the line auth = create(user: user,
provider: auth_hash["provider"],
uid: auth_hash["uid"]) in my Authorization.rb model.
So, I'm very new to this, but am not clear on why this doesn't work. Can someone explain what I'm doing wrong?
Thanks
Mustafa

Use attr_accessible :provider, :uid, :user should work. Or change
auth = create(user: user,
provider: auth_hash["provider"],
uid: auth_hash["uid"])
to
auth = create(user_id: user.id,
provider: auth_hash["provider"],
uid: auth_hash["uid"])

Related

my before_create isn't working even though my method returns nil.

This is my user class:
class User < ActiveRecord::Base
has_secure_password
before_save :valid_email?
validates :username, presence: true,
uniqueness: true
validates :first_name, presence: true
enum role: [ :flyer, :admin ]
def valid_email?
email_checker
end
private
def email_checker
self.email.match(/^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-.]+$/)
end
end
This is my test:
test "if a user has an invalid email, cannot be saved" do
user = build(:user)
user1 = build(:user, email: "here#here#here")
user2 = build(:user, email: "here.here#here")
assert user.save
refute user1.save
refute user2.save
end
The email_checker method does return nil if email is either here#here#here or here.here#here. So what is going on?
Because you just call self.email.match method. If you want to validate filed you should use or create validator.
In your case you can create own custom validator or use e-mail validator from gems (i.e email_validator).

Using has secure password on a rails 4 app

I'm trying to use has_secure_password for user login, I've defined the User mode as below
require 'digest/md5'
class User < ActiveRecord::Base
has_secure_password
before_validation :prep_emailId
before_save :create_avatar_url
validates :emailId, presence: true, uniqueness: true, format: { with: /\A(|(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+#((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6})\z/i }
validates :first_name, presence: true
has_many :projects
belongs_to :nationality
belongs_to :category
scope :sorted, lambda{order("projects.position ASC")}
scope :newest_first, lambda{ "projects.created_at DESC"}
scope :oldest_first, lambda{order("projects.created_at ASC")}
scope :search, lambda{|query|
where(["name LIKE?", "%#{query}%"])
}
private
def prep_emailId
self.emailId = self.emailId.strip.downcase if self.emailId
end
def create_avatar_url
self.avatar_url = "http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(self.emailId)}?s=50"
end
end
I've declared strong parameters on the controller
def user_params
params.require(:user).permit(:category_id, :nationality_id, :first_name,
:last_name, :gender, :date_of_birth, :emailId, :password,
:password_confirmation, password_digest, :avatar_url)
end
Here's my create method.
def create
#user = User.new(user_params)
if #user.save
redirect_to user_path(#user.id)
#notice: "Thanks you for signing up !!!"
else
render ('new')
end
end
The error I'm getting when I try to save is as follows
Password digest missing on new record
Now if I take out attr_accessor from this code as suggested by many on stackoverflow this is what I end up getting.
Mysql2::Error: Data too long for column 'password_digest' at row 1: INSERT INTO `users` (`avatar_url`, `created_at`, `emailId`, `first_name`, `last_name`, `password_digest`, `updated_at`) VALUES ('http://www.gravatar.com/avatar/f76ca3885ff46187f3a216ba566623b9?s=50', '2014-03-17 10:39:01', 'funny#funnier.com', 'funny', 'funnier', '$2a$10$lJp6l70lHepWGz08f4O7luT3kE6Wj7bYzqD3o6G.EErkl0FTbAiHq', '2014-03-17 10:39:01')
You don't need the attr_accessors as has_secure_password handles that and the validation. You'll want password_confirmation in the view not password confirm.

validation fails on SecurePassword Virtual Attributes on custom create at Activerecord Model

I'm trying to mix a custom User authentication mechanism based on SecurePassword with Facebook integration through omniauth-facebook gem.
my app uses Ruby 2.0.0 and Rails 4.0.0.
i tried to follow this guide omniauth and some other articles to came up with something like this for the User and Authentication Models
User model:
class User < ActiveRecord::Base
has_one :user_playlist
has_one :user_info
has_many :band_likes
has_many :song_likes
has_many :band_comments
has_many :song_comments
has_many :authorizations
#many to many relation between User and Band
#todo: make a bands_users migration
has_and_belongs_to_many :bands
has_secure_password
validates :username, presence: true, uniqueness: {case_sensitive: false}, length: {in: 8..64}, format: {with: /\A[a-zA-Z ]+\Z/, message: 'Debe poseer solo letras y espacios.'}
validates :email, presence: true, uniqueness: {case_sensitive: false}, format: {with: /#/, message: 'Dirección de correo inváilda.'}
validates :password, length: {in: 8..24}
validates :password_confirmation, length: {in: 8..24}
def self.create_from_hash!(hash)
create(:email => hash['info']['email'], :username => hash['info']['name'], :password => hash['uid'], :password_confirmation => hash['uid'] )
end
end
Authorization Model:
class Authorization < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id, :uid, :provider
validates_uniqueness_of :uid, :scope => :provider
def self.find_from_hash(hash)
find_by_provider_and_uid(hash['provider'], hash['uid'])
end
def self.create_from_hash(hash, user = nil)
user ||= User.create_from_hash!(hash)
Authorization.create(:user => user, :uid => hash['uid'], :provider => hash['provider'])
end
end
SessionsController
class SessionsController < ApplicationController
def create
auth = request.env['omniauth.auth']
unless #auth = Authorization.find_from_hash(auth)
# Create a new user or add an auth to existing user, depending on
# whether there is already a user signed in.
#auth = Authorization.create_from_hash(auth, current_user)
end
# Log the authorizing user in.
self.current_user = #auth.user
render :text => "Welcome, #{current_user.username}. <br />User saved = #{current_user.save} .<br/>User valid = #{current_user.valid?}.<br />errors= #{current_user.errors.full_messages}"
end
end
The last render was written to check about the fact that my password does not gets validated, it doesn't matter if i use hash['uid'], hash['info']['name'], or whatever.
The reason why i use this values is just because, i will figure out later how to build a random password for the oauth-ed user, but i don't want blank ones nor disable the validations.
but, no matter what value i use, always get only my name and email:
*Welcome, "My Real Name Here.
User saved = false.
User valid = false.
errors= ["Password is too short (minimum is 8 characters)", "Password confirmation is too short (minimum is 8 characters)"]*
When creating users in Rails Console got no problem, just when OAuth tries to create a User with create_from_hash.
also, if i try to assign a non existing value from hash to password fields, it adds the message that can be blank. so, it isn't blank.
and rendering hash['uid'] in controller shows that it's longer than 8.
I Must warn that i'm new to rails, so if you can, explain me with apples xD
Thanks in advance!
finally i came up with this on User model:
def self.create_from_hash!(hash)
self.where(:email => hash.info.email.to_s).first_or_create do |user|
user.email = hash.info.email
user.username = hash.info.name
user.password = hash.uid
user.password_confirmation = hash.uid
end
end
I don't know why the later doesn't work but at least this one works!
Greetings!

hashed password being stored as NULL :Ruby on rails

I am trying to store a password after hashing it but it shows up as NULL in the database.I generated a scaffold for users using password string and name string, and then altered the mysql table to store hashed password instead using this :
ALTER TABLE users CHANGE password hashed_password CHAR(40) NULL;
my model:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :password
validates :name, :uniqueness => true
validates :password, :length => { :in => 6..20 }
def before_create
self.hashed_password = User.hash_password(self.password)
end
def after_create
#password = nil
end
private
def self.hash_password(password)
Digest::SHA1.hexdigest(password)
end
end
I am using Rails 3.2.13.
I think you should use
before_create :hash_the_password
after_create :nil_the_password
def hash_the_password
self.hashed_password = User.hash_password(self.password)
end
def nil_the_password
#password = nil
end
and NOT
#Wrong?
def before_create
...
end
so the callbacks can be the problem.

How to make my method add_friend work in rake db:seed

I'm folowwing Charles Max Wood tutorial on the twitter clone , flitter.
I'm having and error undefined method friendships when I launch rake db:seed .I'am trying to add friend via the rake db:seed task , The method add_friend is define in the User model. But i need help to define the method friendships so that the task can work .Thank you a lot for your help .
Here is the db/seeds.rb file
require 'faker'
require 'populator'
User.destroy_all
10.times do
user = User.new
user.username = Faker::Internet.user_name
user.email = Faker::Internet.email
user.password = "test"
user.password_confirmation = "test"
user.save
end
User.all.each do |user|
Flit.populate(5..10) do |flit|
flit.user_id = user.id
flit.message = Faker::Lorem.sentence
end
3.times do
User.add_friend(User.all[rand(User.count)])
end
end
and there is the user file.
class User < ActiveRecord::Base
# new columns need to be added here to be writable through mass assignment
attr_accessible :username, :email, :password, :password_confirmation
attr_accessor :password
before_save :prepare_password
validates_presence_of :username
validates_uniqueness_of :username, :email, :allow_blank => true
validates_format_of :username, :with => /^[-\w\._#]+$/i, :allow_blank => true, :message => "should only contain letters, numbers, or .-_#"
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
validates_presence_of :password, :on => :create
validates_confirmation_of :password
validates_length_of :password, :minimum => 4, :allow_blank => true
has_many :flits, :dependent => :destroy
has_many :friendships
has_many :friends, :through => :friendships
def self.add_friend(friend)
friendship = friendships.build(:friend_id => friend.id)
if !friendship.save
logger.debug "User '#{friend.email}' already exists in the user's friendship list."
end
end
# login can be either username or email address
def self.authenticate(login, pass)
user = find_by_username(login) || find_by_email(login)
return user if user && user.password_hash == user.encrypt_password(pass)
end
def encrypt_password(pass)
BCrypt::Engine.hash_secret(pass, password_salt)
end
private
def prepare_password
unless password.blank?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = encrypt_password(password)
end
end
end
friendship.rb
class Friendship < ActiveRecord::Base
attr_accessible :friend_id, :user_id
belongs_to :user
belongs_to :friend, :class_name => 'User'
validates_uniqueness_of :friend_id, :scope => :user_id
validates_presence_of :user_id, :friend_id
end
I think what you want to be doing is calling add_friend on the instance user, and not on the class User:
3.times do
user.add_friend(User.all[rand(User.count)])
end
Also your add_friend method should be an instance method, not a class method, so you don't need the self:
def add_friend(friend)
friendship = friendships.build(:friend_id => friend.id)
if !friendship.save
logger.debug "User '#{friend.email}' already exists in the user's friendship list."
end
end
You should define this method as a class method not instance method:
def self.add_friend(friend)
friendship = friendships.build(:friend_id => friend.id)
if !friendship.save
logger.debug "User '#{friend.email}' already exists in the user's friendship list."
end
end

Resources