小编Gag*_*gan的帖子

两个phash之间的mysql汉明距离

我有一个表A,其中有一个列'template_phash'.我存储了400K图像生成的phash.

现在我拍摄一张随机图像并从该图像生成一个镜头.

现在我如何查询,以便我可以从表A获得汉明距离差小于阈值的记录,比如说20.

在SQL中看到了二进制字符串的汉明距离,但无法弄明白.

我想我发现我需要制作一个功能来实现这个但是怎么做?

我的两个phash都在BigInt中,例如:7641692061273169067

请帮我制作这个功能,以便我可以查询

SELECT product_id, HAMMING_DISTANCE(phash1,  phash2) as hd 
FROM A 
WHERE hd < 20 ORDER BY hd ASC;
Run Code Online (Sandbox Code Playgroud)

mysql hamming-distance

10
推荐指数
1
解决办法
4965
查看次数

活动模型序列化程序不使用rails-api gem

我在我的json api项目中使用rails-api gem,为此我使用了活动模型序列化程序gem来序列化我的对象,但是有些对象没有使用活动模型序列化程序进行序列化.

我的serializers文件夹里面有一个MessageSerializer

class MessageSerializer < ActiveModel::Serializer
  attributes :id, :sender_id, :recipient_id, :sender_type, :subj, :body, :status, :sender

  def sender
    object.user.try('username')
  end
end
Run Code Online (Sandbox Code Playgroud)

我的消息控制器如下

class Api::MessagesController < Api::BaseController

  def index
    @messages = current_user.incoming_messages
    render json: @messages, serializer: MessageSerializer
  end

end
Run Code Online (Sandbox Code Playgroud)

但问题是抛出到客户端的序列化对象包含消息模型中的所有字段,即; 它也包含created_at,updated_at字段.

好像它不使用序列化器.

可能出了什么问题?我搜索了很多关于它但没有发现任何帮助我的帖子.

谢谢

ruby-on-rails rails-api active-model-serializers

9
推荐指数
3
解决办法
2498
查看次数

mongoid继承问题

我有这样的模特

class Canvas
  include Mongoid::Document
  field :name
  referenced_in :hero
end

class Browser < Canvas
  field :version, :type => Integer
end

class Hero
  include Mongoid::Document
  field :name
  references_many :canvases
end
Run Code Online (Sandbox Code Playgroud)

如何构建从Hero对象引用的Brower对象.

我想做的就是

h = Hero.create!({:name => 'Aston'})
h.browsers.build
Run Code Online (Sandbox Code Playgroud)

但它给了我一个错误

undefined method `browsers' for #<Hero _id: 4d92c8fc1426960fff000005, name: "Aston">
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

谢谢

inheritance has-many mongoid

7
推荐指数
1
解决办法
818
查看次数

使用Resque和Rspec示例进行测试?

我正在使用Resque处理我的后台作业.我的模型看起来像这样

class SomeClass
  ...
  repo = Repo.find(params[:repo_id])
  Resque.enqueue(ReopCleaner, repo.id)
  ...
end

class RepoCleaner
  @queue = :repo_cleaner

  def self.perform(repo_id)
    puts "this must get printed in console"
    repo = Repo.find(repo_id)    
    # some more action here
  end
end
Run Code Online (Sandbox Code Playgroud)

现在要同步测试我已添加

Resque.inline = Rails.env.test?
Run Code Online (Sandbox Code Playgroud)

在我的config/initializers/resque.rb文件中

这应该调用#perform方法内联而不将其排入Redis并且没有任何Resque回调作为Rails.env.test?在测试环境中返回true.

"this must get printed in console"
Run Code Online (Sandbox Code Playgroud)

在测试时从不打印.我的测试也失败了.

有没有我错过的配置.目前我正在使用

resque (1.17.1)
resque_spec (0.7.0)
resque_unit (0.4.0)
Run Code Online (Sandbox Code Playgroud)

jobs background ruby-on-rails resque

7
推荐指数
1
解决办法
7096
查看次数

在json文件中导出neo4j数据库

我想在JSON文件中导出Neo4j图形数据库.

这是Neo4j Web UI版本中的Export JSON按钮,如下图所示. 在此输入图像描述

但是Neo4j shell中相同任务的等效命令是什么.

谢谢

database json export neo4j

7
推荐指数
1
解决办法
5056
查看次数

创建mongoid rails后跳过验证

我想在创建对象后跳过验证.让我们举个例子

人有很多公司,公司有很多人

人有多个位置,属于人的位置只能有一个有效的位置

展示位置模型有一个验证,用于检查一个人在保存时是否已有活动展示位置.

@placement is active placement
@employment.placement = @person

if @placement.save
  #################
  @person.placements << @placement
  @company.placements << @placement
end
Run Code Online (Sandbox Code Playgroud)

现在,当第一次保存位置时,没有问题,它会被保存.

现在问题来了

@person.placements << @placement
Run Code Online (Sandbox Code Playgroud)

由于此人已通过@ placement.save进行了有效展示位置.

@ person.placements << @placement再次保存@placement,验证将验证错误激活到@placement对象.

有什么方法可以告诉我不要在代码的############区域中进行特定的验证.

或者欢迎任何替代解决方案.

谢谢

validation ruby-on-rails skip

6
推荐指数
1
解决办法
3633
查看次数

mongid嵌入式文档回调

我有一个跟mongoid rails3的以下模型

class Address
  include Mongoid::Document
  embedded_in :person, :inverse_of => :address
  after_validation :call_after_validation
  before_validation :call_before_validation
  before_update :call_before_update
  after_update :call_after_update
  after_create :call_after_create
  before_create :call_before_create

  field :address1
  field :address2

  private
  def call_after_validation
    puts "After validation callback fired."
  end

  def call_before_validation
    puts "Before validation callback fired."
  end

  def call_before_update
    puts "Before update callback fired."
  end

  def call_after_update
    puts "After update callback fired."
  end

  def call_after_create
    puts "After create callback fired."
  end

  def call_before_create
    puts "Before create callback fired."
  end



end

class Person
  include …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails callback mongoid

6
推荐指数
2
解决办法
3822
查看次数

嵌入许多嵌套形式,mongoid

我使用mongoid作为我的数据库.

我的模型是这样的

class Address
  include Mongoid::Document
  embedded_in :person, :inverse_of => :addresses
end

class Person
  include Mongoid::Document
  embeds_many :addresses
end
Run Code Online (Sandbox Code Playgroud)

我在设置动态嵌套人员表单时遇到问题,用户可以在表单中添加多个地址并立即保存所有地址.更新人员嵌套表单时我的哈希是这样的

"person"=>{"name"=>"John", 
"addresses_attributes"=>{"0"=>{"address1"=>"calgary","address2"=>"New York", "id"=>"4cef79f67adf3509280001be"}, 
                         "1"=>{"address1"=>"bhah", "address2"=>"blah", "id"=>"4cef74rdeadf3509280001bf"}}, 
"policy_id"=>"4cef5feb7adf35092800013a", 
"start_date"=>"2010-11-10", "end_date"=>""}
Run Code Online (Sandbox Code Playgroud)

但是根据收到的哈希值,地址没有得到更新.

知道为什么会这样吗?

谢谢

document mongoid

5
推荐指数
1
解决办法
1304
查看次数

为什么jPlayer没有在Firefox中播放我的MP3文件?

我在Firefox 8中使用jQuery Jplayer播放MP3文件时遇到问题.我已经为浏览器安装了最新的flash,我可以看到在Firebug的Flash选项卡中正在下载jplayer.swf文件.

我按此顺序包含以下文件:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<script src="https://raw.github.com/happyworm/jPlayer/master/jquery.jplayer/jquery.jplayer.js" type="text/javascript" charset="utf-8"></script>
Run Code Online (Sandbox Code Playgroud)

我在加载的DOM中有这个:

  $("#jquery_jplayer").jPlayer({
    ready: function () {
      $(this).jPlayer("setMedia", {
        mp3: "http://sound26.mp3pk.com/indian/ladiesvsricky/ladiesvsrickybahl01(www.songs.pk).mp3"

      });
    },
    swfPath: "http://cloudfactory-transcription.s3.amazonaws.com/javascripts/",
    supplied: "mp3",
    volume: 1,
    wmode:"window",
    solution: "html,flash"
  });
Run Code Online (Sandbox Code Playgroud)

我有这个HTML:

<div id="jquery_jplayer" style="height: 0px"></div>
          <div class="jp-audio">
            <div class="jp-type-single">
              <div id="jp_interface_1" class="jp-interface all_rounded_corners">
                <ul class="jp-controls">
                  <li><a href="#" class="jp-play pp" tabindex="1">play</a></li>
                  <li><a href="#" class="jp-pause pp" tabindex="1">pause</a></li>
                  <li><a href="#" class="jp-previous traverse" tabindex="1">Previous</a></li>
                </ul>
                <div class="jp-progress" style = "display:none;">
                  <div class="jp-seek-bar">
                    <div class="jp-play-bar"></div>
                  </div>
                </div>
              </div>
            </div> …
Run Code Online (Sandbox Code Playgroud)

firefox jquery mp3 jplayer

5
推荐指数
1
解决办法
2万
查看次数

关于用户名和子域的用户身份验证

我正在使用devise作为我的身份验证系统.我想用用户名和子域验证用户身份.似乎设计需要同一个表中的用户名和子域字段,而不是我的情况.
我在Company表中有子域字段,而UserAccount表中有用户名和密码.
UserAccount与Company表之间存在references_many关系现在如何使用username和subdomain对用户进行身份验证

请帮帮我.

subdomain devise

4
推荐指数
1
解决办法
1926
查看次数