I'm a newbie and I got some trouble when run rails 3 server with mongodb (using mongoid) in production mode. Everything is ok in development mode, and I cannot figure out what am I missing. Please help me, thank you very much. This is the error:
ActionView::Template::Error (undefined method `map' for nil:NilClass):
2: #control{:style => "float: left;"}
3: %h3= t(:manage_shop)
4: =# debugger
=###### Already checking #shops variable but still got error ######
5: = hidden_field_tag :shop_list, #shops.blank? ? "test" : #shops.to_json
6: = form_tag("/shop", :method => "POST", :id => "frm_create_shop") do
7: %table
8: %tr
app/views/shop/index.html.haml:5:in`_app_views_shop_index_html_haml___1855911541554609468_28040500'
this is my action:
def index
#shops = Shop.all
respond_to do |format|
format.html
end
end
my model:
class Shop
include Mongoid::Document
include Mongoid::Geospatial
field :id, type: Integer
field :name, type: String
field :position, type: Point, :spatial => true, :delegate => true
spatial_scope :position
end
and my production.rb configuration:
Trunk::Application.configure do
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
end
And the second thing is I cannot get anything from db although everything is work well in development mode. If you have any suggestion, please help, thanks.
Related
Today, I had a bug crop up in our Ruby-on-Rails application.
Somehow, the awsaccount_id field of our Device model is getting "marked for change" whenever we access the awsvpc association through the Device model.
To debug the problem, I added several log entries and several extra lines of code (as seen below) to diagnose how the device model instance is getting marked for change.
You may notice that the "dev.changes" method returns a pending change between logging "changes12" and "changes2". The only code that occurs between those log entries is accessing the association with dev.awsvpc.id
Under what circumstances can this ruby code:
Rails.logger.info "Update_Device changes0: #{dev.changes}"
sample_vpc = dev.awsvpc_id
Rails.logger.info "Update_Device changes1: #{dev.changes}"
sample_vpc = Awsvpc.find(dev.awsvpc_id)
Rails.logger.info "Update_Device changes12: #{dev.changes}"
sample_vpc = dev.awsvpc.id
Rails.logger.info "Update_Device changes2: #{dev.changes}"
Cause log entries that look like this:
Update_Device changes0: {}
Update_Device changes1: {}
[1m[36mAwsvpc Load (0.2ms)[0m [1m[34mSELECT "awsvpcs".* FROM "awsvpcs" WHERE "awsvpcs"."id" = $1 LIMIT $2[0m [["id", 644], ["LIMIT", 1]]
↳ app/lib/api/api_get_asset_result_builder.rb:53:in `populate_awssubnet'
Update_Device changes12: {}
[1m[36mCACHE Awsvpc Load (0.0ms)[0m [1m[34mSELECT "awsvpcs".* FROM "awsvpcs" WHERE "awsvpcs"."id" = $1 LIMIT $2[0m [["id", 644], ["LIMIT", 1]]
↳ app/lib/api/api_get_asset_result_builder.rb:55:in `populate_awssubnet'
Update_Device changes2: {"awsaccount_id"=>[66, 644]}
For anyone else who has this problem, I found the problem.
In the models, we had:
Device.rb
belongs_to :awsaccount, inverse_of: :devices, optional: true
belongs_to :awsvpc, inverse_of: :devices, optional: true
Awsvpc.rb
has_many :devices, :class_name => 'Device', dependent: :nullify, inverse_of: :awsaccount
Awsaccount.rb
has_many :devices, :class_name => 'Device', dependent: :nullify, inverse_of: :awsaccount
As you can see, the Awsvpc model should have used inverse_of :awsvpc
Once changed, everything works as expected.
I am getting the following postgres error:
"operator does not exist: jsonb = integer"
I have my application hosted on Heroku and am using a Postgres 9.4 DB. Below is what my accounts controller looks like with the ahoy_matey analytics gem.
Accounts Controller
class AccountsController < ApplicationController
before_filter :authenticate_user!
def index
if current_user.business_id
#business = Business.find(current_user.business_id)
# analytics
#monthly_business_views = ahoy_event_sum("Page Viewed", #business.id)
#monthly_get_websites = ahoy_event_sum("Visited Website", #business.id)
#monthly_get_directions = ahoy_event_sum("Directions Viewed", #business.id)
#monthly_get_fb_shares = ahoy_event_sum("Shared via Facebook", #business.id)
#monthly_get_tweets = ahoy_event_sum("Tweeted via Twitter", #business.id)
#reviews = Review.where(:business_id => #business).all
end
end
...
def ahoy_event_sum(event, business_id)
Ahoy::Event.where(name:event, properties: business_id).count
end
end
Ahoy Events Model
module Ahoy
class Event < ActiveRecord::Base
self.table_name = "ahoy_events"
belongs_to :visit
belongs_to :user
serialize :properties, JSON
end
end
I have intermediate skills and apologize if this question has a simple fix, please provide any details on how I would solve this issue. Thanks!
Hi I followed the Sunil tutorial but I'm getting the following db error:
Completed 500 Internal Server Error in 11.2ms
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
Processing by Refinery::PagesController#home as HTML
Parameters: {"locale"=>:es}
SELECT a.attname, format_type(a.atttypid, a.atttypmod),
ActionView::Template::Error (PG::UndefinedTable: ERROR:
relation "refinery_project_translations" does not exist
2014-12-29T14:14:46.684169+00:00 app[web.1]:
LINE 5: WHERE a.attrelid = '"refinery_project_translati...
2014-12-29T14:14:46.684177+00:00 app[web.1]:
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
Migration file:
class CreateProjectTranslations < ActiveRecord::Migration
def up
::Refinery::Projects::Project.create_translation_table!({:description => :string},
:migrate_data => true)
remove_column :refinery_projects, :description
end
def self.down
add_column :refinery_projects, :description, :string
::Refinery::Projects::Project.drop_translation_table! :migrate_data => true
end
end
after running the migration the I realize is not not creating the the translation table for projects.
Model:
module Refinery
module Projects
class Project < Refinery::Core::BaseModel
self.table_name = 'refinery_projects'
translates :description
attr_accessible :name, :description, :date_started, :date_conclusion,
:position, :relevance, :img_id, :project_id, :status
validates :name, :presence => true, :uniqueness => true
has_many :project_images
has_many_page_images
class Translation
attr_accessible :locale
end
end
end
end
what I'm I missing here?
I solved this issue moving the migration file from the extension to the app migration folder. After that the application was able to create the table for the translation.
Below is a command to generate the extension. If to use it on an existing one, it`ll add all required code to already existing files by overriding them. Be careful with that - commit files before.
rails g refinery:engine Service title:string description:text icon:image --i18n title description
How can a document claim foo.persisted? == true, when foo.class.count == 0?
More importantly, how can I get the document to truly persist?
Update: calling Mongo::Foo.create() from the controller does increment the count. That is expected. Calling create, or new and save, from within a separate module/class does not increment the count.
Update: I have tried changing new/save calls to create
Update: I have also tried #foo_class.with(collection: "mongo_foos").create
[42] pry(#<FoosController>)> foo.class
=> Mongo::Foo
[43] pry(#<FoosController>)> foo.persisted?
=> true
[44] pry(#<FoosController>)> Mongo::Foo.count
=> 0
[47] pry(#<FoosController>)> foo.id
=> "5081a1a786ddc8e035000021"
[48] pry(#<FoosController>)> foo
=> #<Mongo::Foo _id: 5081a1a786ddc8e035000021, _type: nil, foo_daddy_id: nil, old_foo_daddy_id: 219, member_id: "F008AR", unix_time: nil, date_time: 2012-10-19 13:50:54 UTC, submitted: nil, json: nil>
Here's how the document class is defined:
module Mongo
class Foo
include Mongoid::Document
field :foo_daddy_id
field :old_foo_daddy_id, type: Integer
field :member_id, type: String
field :unix_time, type: Integer
field :date_time, type: DateTime
field :submitted, type: Integer
field :json, type: Hash
belongs_to :foo_daddy, class_name: "Mongo::FooDaddy"
embeds_many :foo_dumplings, class_name: "Mongo::FooDumpling"
end
end
The doc is being created with foo.new(params); foo.save:
module FooThingy
module Foo
class Create < ::FooThingy::Base
def initialize(options)
# Sets instance variables used below
end
def execute!
#foo = #foo_class.new({
date_time: DateTime.strptime(#params["submitted_timestamp"], "%m-%d-%Y %H:%M:%S"),
member_id: (#params[:member_id].present? ? #params[:member_id] : nil),
old_foo_daddy_id: #params[:old_foo_daddy_id]
})
embed_foo_dumplings
if #foo.save
return FooThingy::Result.new(success: true, data: { foo: #foo })
else
return FooThingy::Result.new(success: false, data: { foo: #foo })
end
end
private
def embed_foo_dumplings
# Embeds foo_dumplings
end
end
end
end
Here's where I call that module to create the document:
class FoosController < ApplicationController
def create
foo_creator = FooThingy::Foo::Create.new(params: params, foo_class: Mongo::Foo)
foo = foo_creator.execute!
foo = foo.data[:foo]
binding.pry
end
end
This problem, I think, is less interesting than it appears. I've modified my config/mongoid.yml to specify a new database, and everything behaves as expected. Problem was likely due to limitations on 32-bit MongoDB installations.
I have an application on Heroku that uses Carrierwave to upload images to S3. The app is running perfectly on local machine but on Heroku throws the following error and fails the uploading to S3:
TypeError (can't convert Hash into String):
2011-09-23T15:12:07+00:00 app[web.1]: app/controllers/admin/albums_controller.rb:49:in `create'
2011-09-23T15:12:07+00:00 app[web.1]: app/controllers/admin/albums_controller.rb:48:in `create'
That line corresponds to the "if #album.save" instruction.
My Albums controller create action is:
def create
#album = Album.new(params[:album])
respond_to do |format|
if #album.save
format.html { redirect_to(admin_album_path(#album), :notice => 'Àlbum creat correctament.') }
format.xml { render :xml => [:admin, #album], :status => :created, :location => #album }
else
format.html { render :action => "new" }
format.xml { render :xml => #album.errors, :status => :unprocessable_entity }
end
end
end
My Carrierwave initializer:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => APP_CONFIG['storage']['s3_access'],
:aws_secret_access_key => APP_CONFIG['storage']['s3_secret'],
}
config.fog_directory = 'romeu'
config.fog_host = 'http://xxxxx.s3.amazonaws.com'
config.fog_public = true
config.root = Rails.root.join('tmp')
config.cache_dir = 'carrierwave'
end
My image_uploader.rb:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Album Cover version
version :cover do
process :square_resize => [150,150]
end
# Thumb version
version :thumb do
process :square_crop => [80,80]
end
def square_crop(width, height)
manipulate! do |img|
side = [img['width'], img['height']].min
x = (img['width'] - side) / 2
y = (img['height'] - side) / 2
img.crop("#{side}x#{side}+#{x}+#{y}")
img.resize("#{width}x#{height}")
img
end
end
def square_resize(width, height)
manipulate! do |img|
img.resize("#{width}x#{height}")
img
end
end
# Valid list
def extension_white_list
%w(jpg jpeg gif png)
end
end
My config.ru:
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
use Rack::Static, :urls => ['/carrierwave'], :root => 'tmp'
run Encen::Application
I have inspected the #album object and everything seems okay:
_mounters:
:image: !ruby/object:CarrierWave::Mount::Mounter
_memoized_option:
?
- :mount_on
:
column: :image
integrity_error:
options: {}
processing_error:
record: *id001
uploader: !ruby/object:ImageUploader
cache_id: 20110923-0810-1-0644
file: !ruby/object:CarrierWave::SanitizedFile
content_type: image/jpeg
file: /app/tmp/carrierwave/20110923-0810-1-0644/image.jpg
original_filename:
filename: image.jpg
model: *id001
mounted_as: :image
original_filename: image.jpg
versions:
:thumb: !ruby/object:
file: !ruby/object:CarrierWave::SanitizedFile
cache_id: 20110923-0810-1-0644
content_type: image/jpeg
file: /app/tmp/carrierwave/20110923-0810-1-0644/image.jpg
original_filename:
filename: image.jpg
model: *id001
mounted_as: :image
original_filename: image.jpg
parent_cache_id: 20110923-0810-1-0644
versions: {}
:cover: !ruby/object:
cache_id: 20110923-0810-1-0644
file: !ruby/object:CarrierWave::SanitizedFile
content_type: image/jpeg
file: /app/tmp/carrierwave/20110923-0810-1-0644/image.jpg
original_filename:
filename: image.jpg
model: *id001
mounted_as: :image
attributes:
title:
body:
model: *id001
previously_changed: {}
readonly: false
I have spent a bunch of days intending to resolve that error but unsuccessful, what I am missing?
Thanks in advance.
After long days of frustation I have solved the problem. It was such as an stupid thing like the environment vars of S3 access keys on Heroku were incorrectly defined. I don't understand why Fog gem don't gives you more accurate debugging information about that kind of errors.