rake db:migrate fails with globalize gem and rails 4.1.7 - ruby-on-rails-4

I'm trying to use the globalize gem with Rails 4.1.7.
Using latest RVM version, os is Ubuntu 12.04, db is postgresql 9.3.
Following the instructions found in readme.md on github i added the translates directive in my model and scaffolded a migration
class Ingredient < ActiveRecord::Base
translates :name, :description
end
class CreateIngredientTranslations < ActiveRecord::Migration
def up
Ingredient.create.translation_table!({
name: :string,
descripion: :text
}, {
migrate_data: true
})
end
def down
Ingredient.drop_translation_table! migrate_data: true
end
end
Then when i do rake db:migrate i get the following error
PG::UndefinedTable: ERROR: relation "ingredient_translations" does not exsist
LINE 5: WHERE a.attrelid = '"ingredient_translations"...
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"ingredient_translations"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
/home/kranz/.rvm/gems/ruby-2.1.4#recipes/gems/activerecord-4.1.7/lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `async_exec'
This is really weird.
Googling around for this kind of error I found a ticket in github
https://github.com/globalize/globalize/issues/288
opened in Dec 2013 and still not closed which seems very similar; the only solution seems to be a hack in globalize code, but i'd prefer not to monkey patching code at that level.
Is it due to the combination of postgres and globalize or there is any other solution to this?

Related

rails4 deployment logs out all users

got some strange behavior since quite a while. After deployment (with capistarano) all users need to sign in again!
happens in several apps, since Rails 4.x .. here some versions:
Rails: 4.2.5 / 4.2.6
Devise: 3.5.3 / 3.5.6 / 4.2.0
Capistrano: 3.4.0
Ruby: 2.2.1 / 2.3.0
Web-Server: nginx
App-Server: thin (1.6.4 / 1.7.0)
this is really annoying, especially because zero-downtime-deployment makes no sence anymore and remember-me doesn't work at all
models/user
class User < ActiveRecord::Base
..
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable,
:validatable, :confirmable, :lockable, :timeoutable, :omniauthable, :invitable,
:omniauth_providers => CONFIG[:devise_provider]
..
end
initializers/devise
Devise.setup do |config|
config.secret_key = 'xxx-key-xxx'
config.mailer_sender = CONFIG[:mail_system]
config.mailer = 'AccountMailer'
require 'devise/orm/active_record'
config.case_insensitive_keys = [:email]
config.strip_whitespace_keys = [:email]
config.skip_session_storage = [:http_auth]
config.stretches = Rails.env.test? ? 1 : 10
config.invite_for = 0
config.reconfirmable = true
config.expire_all_remember_me_on_sign_out = true
config.password_length = CONFIG[:password_min_length]..CONFIG[:password_max_length]
config.timeout_in = 6.hours
config.reset_password_within = 6.hours
config.sign_out_via = :delete
config.omniauth :facebook, CONFIG[:facebook_id], CONFIG[:facebook_secret], {info_fields: 'email, first_name, last_name, gender', image_size: "large"}
config.omniauth :google_oauth2, CONFIG[:google_id], CONFIG[:google_secret], {
skip_jwt: true,
scope: "email, profile, plus.me",
# prompt: "select_account",
image_aspect_ratio: "square",
image_size: 200
}
end
Most of the apps are running in production, so I don't touch the database while deployment (except for migrations).
Also happens in apps without devise_invitable, so this also don't causes it.
.. thanks for any help! ..
also posted as devise #4277
solved it!
The problem was caused by the only thing I didn't mention: rvmor better rvm1-capistrano3which completely ignores ~/.bachrc, ~/.profile and so on.
I had to put secret_key_base in /etc/environment now it works as expected.
Till now secret_key_base was just a huge pain in the ass for me, because all documented usage didn't work and I had to put secret_key_base in :default_env and also inject it in some monit scripts. ie: to restart sidekiq or thin (therefore it was hardcoded in deploy.rb)
Thanks to surendar, his answer on capistrano 3 + rvm1-capistrano3 rails 4.1 secrets.yml environmental variables issue was the solution.
It still don't seems to be the best solution (but it works), so I'll stay open for better solutions.

Suddenly, every test in our Rails 4 app fails

My co-workers tell me these tests pass when they run them. But I just checked the code out from Bitbucket, and for me, the tests fail. I made sure that my machine has the same version as my co-workers:
Rails 4.2.4
Ruby 2.2.3
rake, version 10.4.2
After some pain with nokogiri, I was able to get "bundle install" to run.
But if I do this:
bin/rake test
Every test fails:
27 runs, 0 assertions, 0 failures, 27 errors, 0 skips
with an error that looks like this:
6) Error:
ProfilesControllerTest#test_#update:
NoMethodError: undefined method `type' for nil:NilClass
Error:
ProfilesControllerTest#test_#update:
NoMethodError: undefined method `each' for nil:NilClass
Here is an example of some failing code:
require 'test_helper'
class ProfilesControllerTest < ActionController::TestCase
test '#update' do
profile = profiles(:no_address_profile)
login(profile)
VCR.use_cassette('essex_street_ny') do
patch :update, profile: {address: '15 Essex street, New York, NY, USA', zipcode: '10002'}
end
updated_profile = Profile.find_by_user_id(profile.user_id)
assert_equal '15 Essex street, New York, NY, USA', updated_profile.address
assert_equal '10002', updated_profile.zipcode
assert_redirected_to root_path
end
test '#update with only zipcode' do
profile = profiles(:no_address_profile)
login(profile)
VCR.use_cassette('only_10002_zipcode') do
patch :update, profile: {zipcode: '10002'}
end
updated_profile = Profile.find_by_user_id(profile.user_id)
assert_equal '10002', updated_profile.zipcode
assert_redirected_to root_path
end
def login(profile)
session[:user_id] = profile.user_id
end
end
So what is really happening here?
UPDATE:
If I do :
rake db:fixtures:load
I get:
rake aborted!
NoMethodError: undefined method `type' for nil:NilClass
It sounds like you need to run migrations in your test environment. You can do that like so:
rake db:migrate RAILS_ENV=test

Rails 4.1, Guard 2.10, and Minitest 5.4.1 -- RuntimeError & Rails::Generators::TestCase

I'm trying to set up Guard with Minitest in a new Rails 4 project. I updated my Gemfile with the following:
group :development do
gem 'guard'
gem 'guard-minitest'
end
And then ran bundle exec guard init minitest.
I've got a pretty simple test like so:
require 'test_helper'
describe ClassToBeTested do
describe "#initialize" do
it "should return a ClassToBeTested object" do
obj = ClassToBeTested.new
obj.must_be_kind_of ClassToBeTested
end
end
end
The class being tested is in app/services/class_to_be_tested.rb.
When I run bundle exec guard -n f I get the following:
11:35:43 - INFO - Guard::Minitest 2.3.2 is running, with Minitest::Unit 5.4.1!
11:35:43 - INFO - Running: all tests
Run options: --seed 36837
# Running:
E
Finished in 0.005190s, 192.6728 runs/s, 0.0000 assertions/s.
1) Error:
ClassToBeTested::#initialize#test_0001_should return a ClassToBeTested object:
RuntimeError: You need to configure your Rails::Generators::TestCase destination root.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
11:35:45 - INFO - Guard is now watching at '/home/sean/Code/Ruby/work/project'
Is there something I'm missing? Something in Guard/Minitest/Rails that needs to be configured to work properly?
Figured it out -- apparently it thought that the class was a generator. Adding the following line to test_helper.rb inside the class TestCase block fixed the issue:
register_spec_type(/ClassToBeTested/, Minitest::Spec)
Is there a way to have all the classes in test/services/ be subclassed under Minitest::Spec, and not the Generator test class?

Splitting User and Profile issue

I'm probably missing something trivial...
I have a User model that 'has_one' UserProfile:
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
before_create :create_remember_token
has_many :user_roles, dependent: :destroy
has_many :roles, through: :user_roles
has_one :user_profile, dependent: :destroy
accepts_nested_attributes_for :user_roles, :user_profile, allow_destroy: true
validates :email, presence: true, format: {with: TextUtils::VALID_EMAIL_REGEX}, uniqueness: {case_sensitive: false}
has_secure_password
validates :password, length: { minimum: 6 }
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.digest(token)
Digest::SHA1.hexdigest(token.to_s)
end
def admin?
in_role?(Role::ADMINISTRATOR)
end
def in_role?(role)
!self.roles.find_by_name(role).nil?
end
private
def create_remember_token
self.remember_token = User.digest(User.new_remember_token)
end
end
and I have a UserProfile model that 'belongs_to' User:
class UserProfile < ActiveRecord::Base
belongs_to :user
has_one :address, dependent: :destroy
accepts_nested_attributes_for :address
validates :first, :last, :ssn, :title, :division, :phone_home, :phone_mobile, :phone_work, :phone_work_ext,
:emergency_phone, :emergency_name, :emergency_relation, presence: true
validates :personal_email, format: { with: TextUtils::VALID_EMAIL_REGEX }
end
When I try to seed the database as such (or any other way where profile is assigned to user):
# Admin
admin = User.create!(email: "admin#work.com", password: "secret", password_confirmation: "secret",
user_profile: UserProfile.new(
first: "Admin",
last: "User",
address_attributes: {line1: "123 Fake Street", line2: "Suite 1", city: "Some City", state: "FL", zip_code: "11111"},
ssn: "123-45-6789",
phone_work: "(111) 222-3333",
phone_work_ext: "110",
personal_email: "test#home.com")
)
I keep getting the following error:
rake aborted!
ActiveRecord::UnknownAttributeError: unknown attribute: user_profile_id
C:/work/Portal/db/seeds.rb:12:in `'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
I'm at a loss as to why this is happening. Any help will be appreciated! I am using Rails 4.1.4, if that matters.
Everything has to be passed as (nested) hash(es):
admin = User.create!({
email: "admin#work.com",
password: "secret",
password_confirmation: "secret",
user_profile_attributes: {
first: "Admin",
last: "User",
address_attributes: {
line1: "123 Fake Street",
line2: "Suite 1",
city: "Some City",
state: "FL",
zip_code: "11111"
},
ssn: "123-45-6789",
phone_work: "(111) 222-3333",
phone_work_ext: "110",
personal_email: "test#home.com"
}
})
Update
Working example:
$ rails new testapplication
create
create README.rdoc
create Rakefile
create config.ru
create .gitignore
create Gemfile
create app
create app/assets/javascripts/application.js
create app/assets/stylesheets/application.css
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create app/views/layouts/application.html.erb
create app/assets/images/.keep
create app/mailers/.keep
create app/models/.keep
create app/controllers/concerns/.keep
create app/models/concerns/.keep
create bin
create bin/bundle
create bin/rails
create bin/rake
create config
create config/routes.rb
create config/application.rb
create config/environment.rb
create config/secrets.yml
create config/environments
create config/environments/development.rb
create config/environments/production.rb
create config/environments/test.rb
create config/initializers
create config/initializers/backtrace_silencers.rb
create config/initializers/cookies_serializer.rb
create config/initializers/filter_parameter_logging.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/session_store.rb
create config/initializers/wrap_parameters.rb
create config/locales
create config/locales/en.yml
create config/boot.rb
create config/database.yml
create db
create db/seeds.rb
create lib
create lib/tasks
create lib/tasks/.keep
create lib/assets
create lib/assets/.keep
create log
create log/.keep
create public
create public/404.html
create public/422.html
create public/500.html
create public/favicon.ico
create public/robots.txt
create test/fixtures
create test/fixtures/.keep
create test/controllers
create test/controllers/.keep
create test/mailers
create test/mailers/.keep
create test/models
create test/models/.keep
create test/helpers
create test/helpers/.keep
create test/integration
create test/integration/.keep
create test/test_helper.rb
create tmp/cache
create tmp/cache/assets
create vendor/assets/javascripts
create vendor/assets/javascripts/.keep
create vendor/assets/stylesheets
create vendor/assets/stylesheets/.keep
run bundle install
Your user account isn't allowed to install to the system Rubygems.
You can cancel this installation and run:
bundle install --path vendor/bundle
to install the gems into ./vendor/bundle/, or you can enter your password
and install the bundled gems to Rubygems using sudo.
$ cd testapplication/
$ bundle install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Installing rake 10.3.2
Installing i18n 0.6.11
Using json 1.8.1
Installing minitest 5.4.1
Installing thread_safe 0.3.4
Installing tzinfo 1.2.2
Installing activesupport 4.1.1
Installing builder 3.2.2
Installing erubis 2.7.0
Installing actionview 4.1.1
Installing rack 1.5.2
Installing rack-test 0.6.2
Installing actionpack 4.1.1
Installing mime-types 1.25.1
Installing polyglot 0.3.5
Installing treetop 1.4.15
Installing mail 2.5.4
Installing actionmailer 4.1.1
Installing activemodel 4.1.1
Installing arel 5.0.1.20140414130214
Installing activerecord 4.1.1
Using bundler 1.6.1
Installing coffee-script-source 1.8.0
Installing execjs 2.2.1
Installing coffee-script 2.3.0
Installing thor 0.19.1
Installing railties 4.1.1
Installing coffee-rails 4.0.1
Installing hike 1.2.3
Installing multi_json 1.10.1
Installing jbuilder 2.1.3
Installing jquery-rails 3.1.1
Installing tilt 1.4.1
Installing sprockets 2.11.0
Installing sprockets-rails 2.1.3
Installing rails 4.1.1
Installing rdoc 4.1.1
Installing sass 3.2.19
Installing sass-rails 4.0.3
Installing sdoc 0.4.1
Installing spring 1.1.3
Installing sqlite3 1.3.9
Installing turbolinks 2.3.0
Installing uglifier 2.5.3
Your bundle is complete!
It was installed into ./vendor/bundle
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:
<= 1.8.6 : unsupported
= 1.8.7 : gem install rdoc-data; rdoc-data --install
= 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!
$ ./bin/rails g model user
invoke active_record
create db/migrate/20140829201904_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
$ ./bin/rails g model user_profile user:references test:integer
invoke active_record
create db/migrate/20140829201916_create_user_profiles.rb
create app/models/user_profile.rb
invoke test_unit
create test/models/user_profile_test.rb
create test/fixtures/user_profiles.yml
$ ./bin/rake db:migrate
== 20140829201904 CreateUsers: migrating ======================================
-- create_table(:users)
-> 0.0012s
== 20140829201904 CreateUsers: migrated (0.0013s) =============================
== 20140829201916 CreateUserProfiles: migrating ===============================
-- create_table(:user_profiles)
-> 0.0010s
== 20140829201916 CreateUserProfiles: migrated (0.0010s) ======================
Then edited the user model:
class User < ActiveRecord::Base
has_one :user_profile
accepts_nested_attributes_for :user_profile
end
UserProfile is already correct:
class UserProfile < ActiveRecord::Base
belongs_to :user
end
and run the create statement:
$ ./bin/rails c
Loading development environment (Rails 4.1.1)
2.1.0 :001 > User.create({user_profile_attributes: {test: 5}})
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "users" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2014-08-29 20:21:41.860036"], ["updated_at", "2014-08-29 20:21:41.860036"]]
SQL (0.4ms) INSERT INTO "user_profiles" ("created_at", "test", "updated_at", "user_id") VALUES (?, ?, ?, ?) [["created_at", "2014-08-29 20:21:41.864624"], ["test", 5], ["updated_at", "2014-08-29 20:21:41.864624"], ["user_id", 1]]
(1.3ms) commit transaction
=> #<User id: 1, created_at: "2014-08-29 20:21:41", updated_at: "2014-08-29 20:21:41">
2.1.0 :002 > User.last.user_profile.inspect
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
UserProfile Load (0.3ms) SELECT "user_profiles".* FROM "user_profiles" WHERE "user_profiles"."user_id" = ? LIMIT 1 [["user_id", 1]]
=> "#<UserProfile id: 1, user_id: 1, test: 5, created_at: \"2014-08-29 20:21:41\", updated_at: \"2014-08-29 20:21:41\">"

(Rails) : NoMethodError: undefined method cost' for BCrypt::Engine:Class

When I learn "Ruby on Rails Tutorial", and I want to create a User on console:
irb(main):001:0> User.create(name:"gsky",email:"k#q.com",
irb(main):002:1* password:"aaaaaa",password_confirmation:"aaaaaa")
then, I getting the following error message:
NoMethodError: undefined method cost' for BCrypt::Engine:Class
from D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activemodel-4.
0.2/lib/active_model/secure_password.rb:104:inpassword='
from D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4
.0.2/lib/active_record/attribute_assignment.rb:42:in public_send'
This is user model:
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
end
Add bcrypt-ruby to your Gemfile as specified below:
gem 'bcrypt-ruby', '3.1.2'
then run bundle update from your project root directory and bundle install
When i saw "Ruby On Rails Tutorial" I have met the same problem,
I solved it by set Gemfile from:
gem 'bcrypt-ruby', '3.0.1'
to:
gem 'bcrypt-ruby', '3.1.2'
then run:
bundle install
Also going for the tutorial/book and having the same problems, I used gem 'bcrypt-ruby', '~> 3.0.0' because of problems with bundle install. After going through secure_password.rb, the problem was in BCrypt::Engine.cost, this method actually doesn't exist.
I changed my gem to gem 'bcrypt-ruby', '~> 3.1.0' which installed bcrypt 3.1.7. Saw a warning message about the gem being renamed and changed it to gem 'bcrypt', '~> 3.1.0' (this part shouldn't matter). After doing bundle install, I was able to see the implementation of Bcrypt::Engine.cost through my IDE and I was able to make my user in rails c.
I want to add that adding ActiveModel::SecurePassword.min_cost = true in test.rb was able to let me make new users if I ran rails c in test environment, but when I added the same line in development.rb, it didn't work.
I think you are learning rails from the tutorial. If you just want to continue and not spent much time on doing the right fix, you can just use the cost as say 10, instead of calling the BCrypt::Engine.cost method.
So replace
cost = BCrypt::Engine.cost
with
cost = 10
This value when used will take less than 200ms to compute and that should be okay.
Tested on mac:
Include this in your gemfile:
gem 'bcrypt', '3.1.11'
run:
xcode-select --install
then run: bundle install
That's it.
Best of lucks