Heroku run rake db:seed is broken - ruby-on-rails

I'm using Refinery CMS with a custom engine and I'm not able to run heroku rake db:seed. It seems that this is causing my app to break in production.
Everything works fine locally, but I'm getting this output when I try to seed my database:
https://gist.github.com/f90b545dc85c44addffd
Any thoughts on what might be causing this. Here's my seeds.rb file:
(Refinery.i18n_enabled? ? Refinery::I18n.frontend_locales : [:en]).each do |lang|
I18n.locale = lang
if defined?(Refinery::User)
Refinery::User.all.each do |user|
if user.plugins.where(:name => 'refinerycms-projects').blank?
user.plugins.create(:name => 'refinerycms-projects',
:position => (user.plugins.maximum(:position) || -1) +1)
end
end
end
url = "/projects"
if defined?(Refinery::Page) && Refinery::Page.where(:link_url => url).empty?
page = Refinery::Page.create(
:title => 'Projects',
:link_url => url,
:deletable => false,
:menu_match => "^#{url}(\/|\/.+?|)$"
)
Refinery::Pages.default_parts.each_with_index do |default_page_part, index|
page.parts.create(:title => default_page_part, :body => nil, :position => index)
end
end
end

In order to fix this I needed to specify ruby '1.9.3' in my gemfile. Apparently this is something related to deploying a Refinery CMS app to Heroku.
Once I deployed to heroku with ruby '1.9.3' in my gemfile I was able to do heroku run rake db:seed no problem.
More info on this here:
https://groups.google.com/forum/?fromgroups=#!topic/refinery-cms/bvGsot-wFxc

Related

Cannot connect to the model by rails sitemap_generator gem?

I want to use this gem (sitemap_generator)
sitemap_generator
To create my sitemap xml file for my site.
So i create sitemap.rb inside config folder
Then i put this code inside
require 'rubygems'
require 'sitemap_generator'
SitemapGenerator::Sitemap.default_host = 'https://xxxx.com/'
SitemapGenerator::Sitemap.create do
# add '/home', :changefreq => 'daily', :priority => 0.9
# add '/contact_us', :changefreq => 'weekly'
add '/'
add '/signup'
add '/login'
Activity.find_each do |activity|
add activity_show_path(activity.id), :lastmod => activity.created_at
end
end
SitemapGenerator::Sitemap.ping_search_engines # Not needed if you use the rake tasks
But when i run
ruby config/sitemap.rb
I always got this
uninitialized constant Activity (NameError)
So how can i fixed this
(I guess the problem from the model)
Thanks!
I always run it through the rake task, try this:
rake sitemap:refresh:no_ping
It's possible the rake task does the magic to make the application code available when that's running.
Update: probably a duplicate of Rails sitemap_generator Uninitialized Constant? (sorry I should have looked first)

Rails acts_as_tenant seed data

I'm using the gem 'acts_as_tenant' in a Rails 3 project.
I would like to seed new data every time a new Tenant is added - but only for the new Tenant.
Is there a way to pass a variable to rake db:seed?
Something like:
rake db:seed -tenant=5
Thanks for the help!
This worked:
$ TENANT=5 rake db:seed
In seed.rb:
statuscode = Statuscode.first_or_create(
:tenant_id => ENV['TENANT'],
:statuscode => 'Completed',
:closed => true
)

Ruby on Rails app; Admin Section not working online

I have recently deployed my Ruby on Rails app from running it locally on my machine to a VPS server. The Administration section isn't working, with username and password. It works locally, but online, I can't get in to administer my user details. The rest of the app seems to be working ok, just not the Admin.
In my db/seeds.rb I have:
Admin.create(:email => "admin#admin.com", :password => "admin_password")
#unless Rails.env.production?
# 1000.times do
# Factory.create(:user)
# end
#end
categories_file = File.join(Rails.root, 'config', 'categories.txt')
if File.exists?(categories_file)
File.open(categories_file, 'r') do |f|
while category = f.gets
Category.create(:name => category)
end
end
end
Is there something I need to do to get it working correctly? Thanks.
Someone ran:
RAILS_ENV=production bundle exec rake db:seed
from the production instance (whatever that is) on the server, and now it's working.

How to configure an extra/different migrations folder

A colleague and I are working in different projects that share some models. So, we are sharing the models through a git submodule.
Additionally, we'd like to be able to also share migrations:
In this way, my colleague's migrations would be in the folder db/migrate/other_db of my project.
How can I configure rails migrations to also run the migrations in this extra folder?
In your config file (config/application.rb for all environments or config/environments/$(environment).rb only for particular environment) add this line:
config.paths['db/migrate'] += 'db/migrate/other_db'
And if you want to change default 'db/migrate' path (config.paths['db/migrate'] is an array with one string 'db/migrate' by default), do this:
config.paths['db/migrate'] = ['db/my_migrate']
Here are default config.paths, which we also can change:
"app" => ["app"],
"app/assets" => ["app/assets"],
"app/controllers" => ["app/controllers"],
"app/helpers" => ["app/helpers"],
"app/models" => ["app/models"],
"app/mailers" => ["app/mailers"],
"app/views" => ["app/views"],
"lib" => ["lib"],
"lib/assets" => ["lib/assets"],
"lib/tasks" => ["lib/tasks"],
"config" => ["config"],
"config/environments" => ["config/environments"],
"config/initializers" => ["config/initializers"],
"config/locales" => ["config/locales"],
"config/routes" => ["config/routes.rb"],
"db" => ["db"],
"db/migrate" => ["db/migrate"],
"db/seeds" => ["db/seeds.rb"],
"vendor" => ["vendor"],
"vendor/assets" => ["vendor/assets"],
"vendor/plugins" => ["vendor/plugins"],
"config/database" => ["config/database.yml"],
"config/environment" => ["config/environment.rb"],
"lib/templates" => ["lib/templates"],
"log" => ["log/development.log"],
"public" => ["public"],
"public/javascripts" => ["public/javascripts"],
"public/stylesheets" => ["public/stylesheets"],
"tmp" => ["tmp"],
Update for Rails 5/6;
Rails 5 recommends setting additional migration paths in your config/database.yml file. It's very easy, see this example;
development:
migrations_paths:
- "db/migrate/other_db"
- "db/migrate/something_else"
ActiveRecord::Migrator.migrations_path= will be deprecated in Rails 6.
based on the answer by Swanand, we can write a migration to load the migrations in an external dir:
class MigrateMetadata < ActiveRecord::Migration
MIGRATIONS_PATH='db/migrate/metadata'
def self.up
Dir["#{MIGRATIONS_PATH}/[0-9]*_*.rb"].
sort.map{|filename|require filename}.flatten.
each{|class_name| const_get(class_name).up}
end
def self.down
Dir["#{MIGRATIONS_PATH}/[0-9]*_*.rb"].sort.reverse.
map{|filename|require filename}.flatten.
each{|class_name| const_get(class_name).down}
end
end
By the way, if you are building a gem to work with Rails, you can place a block like the following in your rail tie to add the gem's own migrations.
root = ... # the path to your gem
initializer :append_migrations do |app|
unless app.root.to_s.match root
app.config.paths["db/migrate"] << File.join(root, 'db/migrate')
end
end
There is no need to copy migrations from your gem with generators if you use this technique.
You can make a method to yield the root directory of your gem with some thing like this...
module MyGemName
def root
File.expand_path '../..', __FILE__
end
module_method :root
end
... in the file lib/my_gem_name.rb in your gem.
I do not know of a very clean way to do it, but the code that runs migrations looks something like:
#migrations ||= begin
files = Dir["#{#migrations_path}/[0-9]*_*.rb"]
migrations = files.inject([]) do |klasses, file|
version, name = file.scan(/([0-9]+)_([_a-z0-9]*).rb/).first
Where,
#migrations_path = 'db/migrate'
So if you change this to read from config file instead, it may work out in your favour. But as I said, this is definitely not a very clean way to do it.
Just add this initializer to your lib/engine.rb:
initializer 'your_engine_name.migrations' do |app|
config.paths['db/migrate'].expanded.each do |expanded_path|
app.config.paths['db/migrate'] << expanded_path
ActiveRecord::Migrator.migrations_paths << expanded_path
if Rake.application.top_level_tasks.empty?
ActiveRecord::Migration.check_pending! if ActiveRecord::Migrator.needs_migration?
end
end
end

NameError when using act_as_ferret

I am getting this error when I am using
acts_as_ferret :fields =>[:competitor], :remote => true
NameError in PartController#index
uninitialized constant PartController::Competitor
My Model
class Competitor < ActiveRecord::Base
validates_presence_of :fee_earner_id, :notes
belongs_to :fee_earner
belongs_to :country
belongs_to :state
belongs_to :user
acts_as_ferret :fields =>[:competitor], :remote => true
end
My controller
class PartController < ApplicationController
def index
#proscribeds = Competitor.paginate(:all,
:order => sort ,
:page => params[:page],
:per_page => 70 )
end
end
Its working fine in localhost but when I deploy it in the server than I get this error.
act_as_ferret is working good with other models. I don't know why this is not working with only Competitor model.
These might seem like simplistic suggestions, but here's what comes to mind:
Do you have the same OS on localhost and the server? I've been burned by little discrepancies like the differences between how Unix and Windows handle mixed case pathnames.
Have the most updated versions of all your files been moved? Can you do a diff and ensure that there isn't a missing config file or environment variable or something?
I know those suggestions aren't really Rail/Ruby-specific, but I've found that little configuration problems tend to give me more headaches than actual code errors do.
Good luck!
This could be a problem with the bin/ferret_server file on the remote drb server you are running. What it is complaining about is that it knows nothing about the PartController::Competitor model. This is because the ferret-server does not eager load all the Rails classes like Rails does by default.
I had a similar issue and the solution was to also require the Rails config/environment file as well as the Rails config/application file.
Something like this:
begin
ENV['FERRET_USE_LOCAL_INDEX'] = 'true'
if env = $ferret_server_options['environment']
ENV['RAILS_ENV'] = env
end
# determine RAILS_ROOT unless already set
root = File.expand_path(determine_rails_root)
begin
require File.join(root, 'config', 'application')
# Also require environment to eager load Rails classes
require File.join(root, 'config', 'environment')
rescue
puts "Error booting your rails app at #{root}: #{$!}\n#{$!.backtrace.join("\n")}"
raise $!
end
puts "Rails.root: #{Rails.root}"
puts "Rails.env: #{Rails.env}"
require 'acts_as_ferret/server/server'
ActsAsFerret::Server::Server.new.send($ferret_server_action)
rescue Exception => e
$stderr.puts(e.message)
$stderr.puts(e.backtrace.join("\n")) if $ferret_server_options['debug']
exit(1)
end

Resources