test error:-in Ruby on rails - ruby-on-rails

I'm working with ruby on rails and I'm having a problem while running a "ruby -Itest test/models/user_test.rb". I get an error when I make changes in the regular expression /^[a-zA-Z0-9-]+$/ and now I am stuck in this problem and I don't know how to get over it.
My user.rb file:-
validates :profilename, presence: true,
uniqueness: true, format: { with: /^[a-zA-Z0-9-]+$/ , multiline: true,
message: 'Must be formatted correctly.' }
Running test:-
$ ruby -Itest test/models/user_test.rb
DL is deprecated, please use Fiddle
Run options: --seed 50723
Running:
.....#<ActiveModel::Errors:0x4ecc8a0 #base=#<User id: nil, first_name: "sarah", last_name: "gupta", profile_name: "S_arah-1", email: "sarah089#gma
il.com", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at
: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil>, #messages={}>
E..
Finished in 0.380267s, 15.7784 runs/s, 28.9270 assertions/s..
1) Error: UserTest#test_a_user_can_have_a_correctly_formatted_profile_name: ArgumentError: wrong number of arguments (0 for 1..2) test/models/user_test.rb:41:in `block in <class:UserTest>'
6 runs, 11 assertions, 0 failures, 1 errors, 0 skips
Here is the code for a_user_can_have_a_correctly_formatted_profile_name
test "a user can have a correctly formatted profile name" do
user = User.new(first_name: 'sarah', last_name: 'gupta', email: 'sarah089#gmail.com')
user.password = user.password_confirmation = 'welcome'
user.profile_name = 's_arah-1'
assert.user.valid?
end
Thanks for your help!

It looks like the answer to your question is in the file user_test.rb. Try checking where test_a_user_can_have_a_correctly_formatted_profile_name is defined, and where it's called: according to the error, it's being called without the required one or two arguments. I'm guessing you need to give it a test username.

Related

Rails upgrade 3.2.* to 4 Does not show all user attributes in rails console

Kind of an odd bug I am running into. Working on upgrading a Rails app from 3.2.22.1 tp 4.2.11.3. Everything is working pretty well. Test suite is passing on green. However when I go into the rails console and do user = User.new or list the attributes using User.new.attributes I am only getting a few of the fields returned.
Specifically
{"_id"=>BSON::ObjectId('XXxxXXxxxxXXXxXX'),
"email"=>"",
"encrypted_password"=>"",
"sign_in_count"=>0,
"time_zone"=>"Central Time (US & Canada)",
"admin"=>false}
This list should be much larger, for example, it is excluding all Devise fields like last_sign_in_at or any of them. Here is what it looks like, the same command, run on our production server which is the previous version of rails
_id: xxxXXXxxxXXXXxXXXXxx,
invited_by_type: nil,
invited_by_field: nil,
invited_by_id: nil,
email: "",
encrypted_password: "",
reset_password_token: nil,
reset_password_sent_at: nil,
remember_created_at: nil,
sign_in_count: 0,
current_sign_in_at: nil,
last_sign_in_at: nil,
current_sign_in_ip: nil,
last_sign_in_ip: nil,
confirmation_token: nil,
confirmed_at: nil,
confirmation_sent_at: nil,
unconfirmed_email: nil,
invitation_token: nil,
invitation_created_at: nil,
invitation_sent_at: nil,
invitation_accepted_at: nil,
invitation_limit: nil,
name: nil,
time_zone: "Central Time (US & Canada)",
admin: false
I am worried that there is something failing silently and I am not detecting it. I can save the unlisted values in the console, but it does not show them to me, which is not how it worked previously.
Anyone understand why this is happening?
specific versions are:
mongid -> 5.0.1
devise -> 4.7.2
Rails -> 4.2.11.3
Latest version of Mongoid does not include attributes that were never written in attributes.
Unless you have references to documentation that claims that Mongoid would return attributes that were never written, you are experiencing expected behavior.

Rails 4 - mapping

I have a user model and a profile model.
The associations are:
user has_one :profile
profile belongs_to :user
I have a user model which I am trying to use to create a profile when a new user is first created. I only want to run this method if the user is new.
I have this callback method in user.rb:
after_create :build_profile
def build_profile
Profile.create(user: self) # Associations must be defined correctly for this syntax, avoids using ID's directly.
# Profile.save
end
When I save this and go through my authentication process (using devise & omniauth), I get this runtime error:
RuntimeError (Could not find a valid mapping for [#<User id: 1, first_name: "Me", last_name: "Ma", email: "me#gmail.com", encrypted_password: "$2a$10$KCZn..HS2GuGjvlcNtfAH/28.DQoWetO...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 16, current_sign_in_at: "2015-12-15 09:57:14", last_sign_in_at: "2015-12-15 09:27:07", current_sign_in_ip: #<IPAddr: IPv4:49, last_sign_in_ip: #<IPAddr: IPv4:49.195>, confirmation_token: nil, confirmed_at: "2015-12-15 09:02:58", confirmation_sent_at: "2015-12-15 08:42:48", unconfirmed_email: nil, failed_attempts: 0, unlock_token: nil, locked_at: nil, created_at: "2015-12-09 23:53:20", updated_at: "2015-12-15 09:57:14", image: nil>, #<Profile id: 1, user_id: 1, title: nil, hero_image: nil, overview: nil, occupation: nil, external_profile: nil, working_languages: nil, created_at: "2015-12-18 21:58:49", updated_at: "2015-12-18 21:58:49">]):
I'm not sure what this mapping error means.
I have a user_id integer in my profiles table and it seems to be referencing the relevant user id properly.
I tried adding profile.save to the build_profile method, but the same error is generated, so I don't thing that helps.
Can anyone see what's going wrong?

Testing Rails views in RSpec: why does it route to "show" when I want to test "index"?

I have this very basic view spec in spec/views/users/index_spec.rb:
require 'rails_helper'
RSpec.describe "users/index", type: :view do
before(:each) do
#user = create(:user)
assign(:users, [#user])
end
it "renders a list of users" do
render
expect(page).to have_selector "tr##{dom_id(#user)}"
end
end
When executing it, it's telling me the following:
Failures:
1) users/index renders a list of users
Failure/Error: render
ActionView::Template::Error:
No route matches {:action=>"show", :controller=>"users", :id=>nil, :locale=>#<User id: 1, name: "Rosalinda Dach", email: "marilie#leffler.ca", encrypted_password: "$2a$04$G/z6lbFUpnh9FD3bymYBE.LrJK3acKr4TsURgCq7B77...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmation_token: nil, confirmed_at: "2015-06-03 14:33:11", confirmation_sent_at: nil, unconfirmed_email: nil, failed_attempts: 0, unlock_token: nil, locked_at: nil, created_at: "2015-06-03 14:33:12", updated_at: "2015-06-03 14:33:12", avatar: nil>} missing required keys: [:id]
# /Users/josh/.rvm/gems/ruby-2.1.0#a4aa2/gems/actionpack-4.2.1/lib/action_dispatch/journey/formatter.rb:46:in `generate'
# /Users/josh/.rvm/gems/ruby-2.1.0#a4aa2/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:727:in `generate'
# /Users/josh/.rvm/gems/ruby-2.1.0#a4aa2/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:758:in `generate'
# /Users/josh/.rvm/gems/ruby-2.1.0#a4aa2/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:801:in `url_for'
# /Users/josh/.rvm/gems/ruby-2.1.0#a4aa2/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:280:in `call'
# /Users/josh/.rvm/gems/ruby-2.1.0#a4aa2/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:345:in `block (2 levels) in define_url_helper'
Why does the test route to users#show instead of users#index?
You can change your code:
RSpec.describe "users/index.html.erb", type: :view do
Since I didn't find a suitable solution, I decided to create my own online Markdown editor with copy and paste: PIMP (Pasteable Images, Markdown, Pandoc).
http://pimp.suhail.uberspace.de/en
It's not yet looking very great yet, but it offers everything you need to create semantically awesome documents using Pandoc's very powerful Markdown, paste images directly from clipboard into the text, and export the documents to various formats (at the time being Docx, Epub, Odt, maybe later also PDF).
At the time being, to use PIMP, one has to create a user account.
The whole project is rather a proof of concept than a real project, but if people like and use it, I would be happy to make it a real project.

What's the difference between Rails dup and clone methods? [duplicate]

This question already has answers here:
What's the difference between Ruby's dup and clone methods?
(6 answers)
Closed 7 years ago.
I need to know the difference between Rails dup and clone methods, because dup duplicates the id attribute and clone doesn't:
juan:~/alhambra$ rails c
Loading development environment (Rails 3.0.1)
1.9.3-p551 :001 > #user=User.last
=> #<User id: 2, email: "ferbad12#hotmail.com", encrypted_password: "$2a$10$/Fsz8DZ9PQbReTU1.wyxS.m3IOxZSV3siKDrrtUJdupz...", password_salt: "$2a$10$/Fsz8DZ9PQbReTU1.wyxS.", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2015-05-06 23:34:20", last_sign_in_at: "2015-05-06 23:34:20", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2015-05-06 23:33:37", updated_at: "2015-05-06 23:34:20">
1.9.3-p551 :002 > #user.clone
=> #<User id: nil, email: "ferbad12#hotmail.com", encrypted_password: "$2a$10$/Fsz8DZ9PQbReTU1.wyxS.m3IOxZSV3siKDrrtUJdupz...", password_salt: "$2a$10$/Fsz8DZ9PQbReTU1.wyxS.", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2015-05-06 23:34:20", last_sign_in_at: "2015-05-06 23:34:20", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2015-05-06 23:33:37", updated_at: "2015-05-06 23:34:20">
1.9.3-p551 :003 > #user.dup
=> #<User id: 2, email: "ferbad12#hotmail.com", encrypted_password: "$2a$10$/Fsz8DZ9PQbReTU1.wyxS.m3IOxZSV3siKDrrtUJdupz...", password_salt: "$2a$10$/Fsz8DZ9PQbReTU1.wyxS.", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2015-05-06 23:34:20", last_sign_in_at: "2015-05-06 23:34:20", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2015-05-06 23:33:37", updated_at: "2015-05-06 23:34:20">
In rails 3.0, dup and clone performed essentially opposite roles as to what they do now. From ActiveRecord::Base:
Cloned objects have no id assigned and are treated as new records. Note that this is a "shallow" clone as it copies the object's attributes only, not its associations. The extent of a "deep" clone is application specific and is therefore left to the application to implement according to its need.
While it can be seen in the same file that dup simple copied the record and its attributes:
def dup
obj = super
obj.instance_variable_set('#attributes', #attributes.dup)
obj
end
This differs from current rails 4, which defines dup and clone to more follow the note from the ruby docs, noted in a similar question not specific to rails.
In general, clone and dup may have different semantics in descendent classes. While clone is used to duplicate an object, including its internal state, dup typically uses the class of the descendent object to create the new instance.
As can be seen from the from the more current ActiveRecord source:
##
# :method: clone
# Identical to Ruby's clone method. This is a "shallow" copy. Be
# warned that your attributes are not copied. That means that modifying
# attributes of the clone will modify the original, since they will both
# point to the same attributes hash. If you need a copy of your attributes
# hash, please use the #dup method.
#
# user = User.first
# new_user = user.clone
# user.name # => "Bob"
# new_user.name = "Joe"
# user.name # => "Joe"
#
# user.object_id == new_user.object_id # => false
# user.name.object_id == new_user.name.object_id # => true
#
# user.name.object_id == user.dup.name.object_id # => false
##
# :method: dup
# Duped objects have no id assigned and are treated as new records. Note
# that this is a "shallow" copy as it copies the object's attributes
# only, not its associations. The extent of a "deep" copy is application
# specific and is therefore left to the application to implement according
# to its need.
# The dup method does not preserve the timestamps (created|updated)_(at|on).
The tutorial explains this, everything opposite what my console displays
p1 = Post.create(title: 'Post 1', message: 'Amazing message')
p3 = p1.clone
p3.title = "This is now p3"
p1 #=> #<Post id: 1, title: "Post 1", message: "Amazing message", created_at: "2014-07-01 19:45:44", updated_at: "2014-07-01 19:45:44">
p3 #=> #<Post id: nil, title: "This is now P3", message: "Amazing message", created_at: nil, updated_at: nil>

Ruby validation passes in console, fails in rake task

This is my EmailContact model:
class EmailContact < ActiveRecord::Base
validates :email, :presence => true, :email => true
end
I am using the ruby gem valid_email.
I run the following in my rails console, in the same environment as my rake task I will show later:
>> email_contact = EmailContact.new(:email => 'a253545#gmail.com')
>> email_contact.valid?
true
So, as you can see, in the rails console I am building an EmailContact and it is valid.
Then I run this in my rake task:
list_entity = {:branch=>"Nashua Branch-YMCA of Greater Nashua", :branch_id=>"485", :call_type=>nil, :client_id=>"2264", :client_name=>"YMCA of Greater Nashua", :date_of_birth=>nil, :email=>"a253545#gmail.com", :first_name=>"Sridhar", :last_name=>"Tipirneni", :list_entity_id=>"277795", :mem_id=>"4085008", :mem_unit_id=>"2138728", :member_id=>"0213262-01", :membership_type=>"Dual 2 Adult Family", :membership_type_id=>"5203", :most_recent_join_date=>nil, :old_membership_type=>nil, :phone_number=>"(970)456-1010", :primary_language=>"English", :termination_date=>nil, :termination_reason=>nil, :unit_id=>"0213262", :unit_type=>nil, :visits=>nil, :"#i:type"=>"c:NpsListEntityDto"}
email_contact = EmailContact.new(list_entity.except(:"#i:type"))
puts email_contact.valid?
This returns false. The only validation, at all, is the email. Why does this email validate successfully in my console but fail in my rake task?
FYI, when I remove :email => true from my EmailContact model and only validate the presence of an :email, they both work fine. So the issue is definitely within the :email => true piece of my validation, but I don't understand why it passes in one place and fails in another.
EDIT
In my console, my model looks like this when using the full list_entity:
#<EmailContact id: nil, branch: "Nashua Branch-YMCA of Greater Nashua", branch_id: 485, call_type: nil, client_id: 2264, client_name: "YMCA of Greater Nashua", date_of_birth: nil, email: "a253545#gmail.com", first_name: "Sridhar", last_name: "Tipirneni", list_entity_id: 277795, mem_id: "4085008", mem_unit_id: "2138728", member_id: "0213262-01", membership_type: "Dual 2 Adult Family", membership_type_id: 5203, most_recent_join_date: nil, old_membership_type: nil, phone_number: "(970)456-1010", primary_language: "English", termination_date: nil, termination_reason: nil, unit_id: "0213262", visits: nil, loaded_at: nil, failed_at: nil, unit_type: nil, created_at: nil, updated_at: nil, list_id: nil>
In my rake task, when I run email_contact.inspect, this is returned:
#<EmailContact id: nil, branch: "Nashua Branch-YMCA of Greater Nashua", branch_id: 485, call_type: nil, client_id: 2264, client_name: "YMCA of Greater Nashua", date_of_birth: nil, email: "a253545#gmail.com", first_name: "Sridhar", last_name: "Tipirneni", list_entity_id: 277795, mem_id: "4085008", mem_unit_id: "2138728", member_id: "0213262-01", membership_type: "Dual 2 Adult Family", membership_type_id: 5203, most_recent_join_date: nil, old_membership_type: nil, phone_number: "(970)456-1010", primary_language: "English", termination_date: nil, termination_reason: nil, unit_id: "0213262", visits: nil, loaded_at: nil, failed_at: nil, unit_type: nil, created_at: nil, updated_at: nil, list_id: nil>
As you can see, they are both the exact same - The console model is valid, the rake model is invalid.
EDIT 2
I am using the valid_email gem, mentioned above. Here is the filepath:
/Users/luigi/.rvm/gems/ruby-2.0.0-p247#hub/gems/valid_email-0.0.4/lib/valid_email/email_validator.rb
All of my other gems are stored here as well it seems like.
It may also be worth mentioning that I get this warning before the validation fails:
[deprecated] I18n.enforce_available_locales will default to true in
the future. If you really want to skip validation of your locale you
can set I18n.enforce_available_locales = false to avoid this message.
20 hours later, I found the issue.
Using savon, all of the strings returned in my hash were being converted to a datatype of Nori::StringWithAttributes. The encoding was the same (UTF-8), but the class was different.
Running email_contact.email = email_contact.email.to_s prior to checking if the model is valid solves the issue.

Resources