我正在尝试在我的第一个rails应用程序中实现Paperclip,我碰巧使用rails 3和mongodb与mongomapper.
我按照本指南将所有事情放在一起工作
正如博客文章所暗示的那样,我将paperclip放入config/initializers目录,我安装了gem,gem在gemfile中(rails 3右边),我运行了捆绑器.
在我的用户类中,我添加了
require 'paperclip'
当我加载应用程序时,我收到以下错误,
undefined method 'has_attached_file' for User:Class
回形针文件如下所示
module Paperclip
module ClassMethods
def has_attached_file name, options = {}
include InstanceMethods
write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
attachment_definitions[name] = {:validations => []}.merge(options)
after_save :save_attached_files
before_destroy :destroy_attached_files
define_callbacks :before_post_process, :after_post_process
define_callbacks :"before_#{name}_post_process", :"after_#{name}_post_process"
define_method name do |*args|
a = attachment_for(name)
(args.length > 0) ? a.to_s(args.first) : a
end
define_method "#{name}=" do |file|
attachment_for(name).assign(file)
end
define_method "#{name}?" do
attachment_for(name).file?
end
validates_each name, :logic => lambda {
attachment … 我有一天的模型,每天都包含一个标签哈希.
class Day
include MongoMapper::Document
key :tags, Hash
...
end
Run Code Online (Sandbox Code Playgroud)
标签哈希可能看起来像这样{"a"=> 4,"b"=> 1,"c"=> 1}
我想写一个查询,可以找到标签键等于'a'的所有日子.
Day.where('tags.keys' => "a")
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为键实际上不是哈希中的键,我猜我不能只使用键方法.
我真的想知道是否有办法查询哈希的键,否则我将不得不创建一个数组来存储键和查询.
tags = {"a"=>4, "b"=>1, "c"=>1, "names" => ["a", "b", "c"]}
Day.where('tags.names' => "a") #This would work find, but is not what I want
Run Code Online (Sandbox Code Playgroud) 我在MongoDB中存储文件(来自远程API)有问题,我使用的是Ruby 1.9
class Foo
include ::MongoMapper::Document
key :bar, String
end
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:"\ xFF"从ASCII-8BIT到UTF-8
我尝试了以下事项:
foo.bar = pdf_data.encode('UTF-8')
foo.bar = pdf_data.force_encoding('UTF-8')
foo.bar = pdf_data.ensure_encoding('UTF-8',
:external_encoding => :sniff,
:invalid_characters => :transcode
) # with github.com/Manfred/Ensure-encoding
Run Code Online (Sandbox Code Playgroud)
好吧,其中任何一个都有效,我在保存电话时收到错误...
我在网上看,但我没有找到任何明确的反应(或至少解决我的问题)......任何想法我应该做什么来能够存储它?
我按照这些说明设置了一个新的Rails应用程序.我生成了一个新的控制器并添加resources :tickets到routes文件中.
Hexapoda::Application.routes.draw do
resources :tickets
end
Run Code Online (Sandbox Code Playgroud)
这是控制器(`/app/controllers/tickets_controller.rb').
class TicketsController < ApplicationController
def index
@tickets = Ticket.all
end
end
Run Code Online (Sandbox Code Playgroud)
然后我添加了一个新的模式Ticket在/app/models/ticket.rb.
class Ticket
include MongoMapper::Document
key :summary, String, :required => true
end
Run Code Online (Sandbox Code Playgroud)
这是视图(/app/views/index.html.erb):
<h1>Tickets#index</h1>
<p>Find me in app/views/tickets/index.html.erb</p>
Run Code Online (Sandbox Code Playgroud)
现在,当我进入/tickets浏览器时,收到错误消息.
TicketsController #index中的NoMethodError
未定义的方法`key?' 为零:NilClass
我不知道是怎么回事.可能是什么问题呢?我正在使用Rails 3.2.5和MongoMapper 0.11.1.
我正在使用Update和FindAndModify但现在我已经读过Update和FindAndModify是原子的(http://docs.mongodb.org/manual/tutorial/model-data-for-atomic-operations/),
因此,如果两者都可以完成相同的工作,通过查询项目并更新它,那么有什么区别?
我在StackOverflow上找到了几个答案,但没有一个提到Update也是原子的:在MongoDB中findAndModify和update有什么区别?
如果我有:
class Post
include MongoMapper::Document
has_many :comments
end
Run Code Online (Sandbox Code Playgroud)
如果我做:
class Comment
include MongoMapper::EmbeddedDocument
belongs_to :post # relevant part
end
Run Code Online (Sandbox Code Playgroud)
这是使用_root_document/ 创建关联_parent_document,还是必须添加(冗余)key :post_id?
我有一个小的rake任务,它只是将一个新的延迟作业放入队列中.我插入了一个调试行
desc 'Start processing new rss feed articles'
task :process_new_articles => :environment do
config = RSS_CONFIG
feeds = config['rss_feeds']
puts Article.all
feeds.each do |feed|
Delayed::Job.enqueue ProcessNewArticlesJob.new(feed, config['settings'])
end
end
Run Code Online (Sandbox Code Playgroud)
它似乎正在正确加载配置信息.但是我在运行任务时遇到"未初始化的常量文章"错误.文章是MongoMapper模型.我已经验证了与数据库的连接(在MongoLab上)可以正常工作.
这一切在当地很有效.
-
更奇怪的是,使用"heroku run console"引用Article.all工作得很好.
-
堆栈跟踪显示不多:
2011-12-04T22:33:02+00:00 app[start.1]: rake aborted!
2011-12-04T22:33:02+00:00 app[start.1]: uninitialized constant Article
Run Code Online (Sandbox Code Playgroud) 我非常想使用这里讨论的mongo geoNear命令.
这是我在rails控制台中输入的命令,附带错误消息.
MongoMapper.database.command({ 'geoNear' => "trips", 'near' => [45,45]})
Mongo::OperationFailure: Database command 'geoNear' failed:
(errmsg: 'more than 1 geo indexes :('; ok: '0.0').
Run Code Online (Sandbox Code Playgroud)
我无法理解错误信息,据说不可能有超过1个地理索引,我确信我只创建了一个.
根据这个stackoverflow问题,我相信我正确地写了查询.有谁知道这个错误信息?我将如何破坏和重新创建索引?
我使用rails 3.1与mongodb v2.0和mongo ruby gem v1.5.1.
如何使用MongoMapper 查询distinct?我的查询是:
subscribedToThread = Comment.where(:subscribe_thread => 1).all
但这将返回许多具有相同的对象user_id.我需要回报一个截然不同的user_id.这可能吗?
我得到了来自服务器的一些记录有10分钟的时间间隔(1小时,我会得到6个文件),我想要做的地图减少在接下来的几个小时,每1小时,我将不得不做下一组的地图减少6个文件与去年小时文件我将如何解决这个问题?帮助我在过去1个月里混淆了frm谢谢你Sushil Kr Singh