小编chr*_*san的帖子

如何在Rails中有条件地构建ActiveRecord查询?

我正在尝试在我的rails应用程序(仍然是菜鸟)中构建搜索结果页面,我无法弄清楚如何以rails方式构建查询.

例如,如果没有参数,我想返回所有结果.如果用户在搜索表单中传递1到n个可选参数,我想将它们添加到查询中.

接下来,如果他们有一个指定"price desc"或"year_built desc"的排序,或者甚至是两者的组合.

最后使用will_paginate来分隔结果

# default to all listings
@listings = Mls.all

@listings.where("listing_price > ?", params[:listing_price]) unless params[:listing_price].blank?
# ... bunch of other search options ...
@listings.where("property_type = ?", params[:property_type]) unless params[:property_type].blank?

# order
@listings.order("some order by param") if some sort param
@listings.order("some order by param") if some other sort param

# paginate results
@listings.paginate(:page => params[:page])
Run Code Online (Sandbox Code Playgroud)

这样做是否有"Rails"方式?

activerecord ruby-on-rails ruby-on-rails-3

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

git连接被遗弃,致命:无法从远程存储库中读取

我正在尝试使用Pageant for ssh key克隆cygwin或GitBash(msy​​sgit)中的repo.我收到以下错误:

$ git clone git@github.com:username/your-project.git
Cloning into 'your-project'...
The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
Connection abandoned.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Run Code Online (Sandbox Code Playgroud)

然而,当尝试使用gitub调试ssh连接时,它表示可行

$ ssh -T git@github.com
Warning: Permanently added the RSA host key for IP …
Run Code Online (Sandbox Code Playgroud)

git putty plink pageant

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

如何在MySQL for Mac上设置默认端口

在OSX上,当我尝试使用以下内容时:

resources.db.adapter = "pdo_mysql"
resources.db.params.dbname = "myDb"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = ""
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Library/WebServer/sites/testweb/library/Zend/Db/Adapter/Pdo/Abstract.php on line 129

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /Library/WebServer/sites/testweb/library/Zend/Db/Adapter/Pdo/Abstract.php:129
Stack trace:
#0 /Library/WebServer/sites/testweb/library/Zend/Db/Adapter/Pdo/Abstract.php(129): PDO->__construct('mysql:dbname=gl...', 'myDb', '', Array)
#1 /Library/WebServer/sites/testweb/library/Zend/Db/Adapter/Pdo/Mysql.php(96): Zend_Db_Adapter_Pdo_Abstract->_connect()
#2 /Library/WebServer/sites/testweb/library/Zend/Db/Adapter/Abstract.php(858): Zend_Db_Adapter_Pdo_Mysql->_connect()
#3 /Library/WebServer/sites/testweb/library/Zend/Db/Adapter/Abstract.php(928): Zend_Db_Adapter_Abstract->quote('asdf@asd...', NULL)
#4 /Library/WebServer/sites/testweb/library/Zend/Db/Select.php(1000): Zend_Db_Adapter_Abstract->quoteInto('email = ?', 'asdf@asd...', NULL)
#5 /Library/WebServer/sites/testweb/library/Zen in /Library/WebServer/sites/testweb/library/Zend/Db/Adapter/Pdo/Abstract.php …
Run Code Online (Sandbox Code Playgroud)

mysql zend-framework zend-db

4
推荐指数
2
解决办法
8655
查看次数

将播放头搜索到Flash Video中的特定位置?

如果您在Flash中加载FLVPlayback组件或在Flex中加载VideoDisplay并尝试搜索特定位置或将playheadTime设置为特定位置,则电影总是向上或向下舍入非常大的数量

在Flash中,将FLVPlayback组件拖到舞台并将源设置为http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/examples/assets/phone.flv 然后尝试video.seek (6)或video.playheadTime = 6;

它只会转到10.同样,如果你使用他们的内置洗涤器,唯一的间隔是0,5或10.所以最多你看到的擦洗框架是3

以下是具有相同结果的flex示例.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:media="com.synapsegroup.media.*">

    <mx:Script>
        <![CDATA[
            import mx.events.MetadataEvent;
            import mx.events.SliderEvent;
            import mx.events.VideoEvent;

            protected function scrubber_changeHandler(event:SliderEvent):void
            {               
                trace(event.value);
                video.playheadTime = event.value;
            }

            protected function video_playheadUpdateHandler(event:mx.events.VideoEvent):void
            {               
                //scrubber.value = event.playheadTime;
                //http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/examples/assets/phone.flv
            }

        ]]>
    </mx:Script>

    <mx:Panel>
        <mx:VideoDisplay id="video" source="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/examples/assets/phone.flv" minWidth="200" minHeight="200"
                         autoPlay="false"
                         />
        <mx:ControlBar>
            <mx:Button label="Play" click="video.play();"/>
            <mx:Button label="Stop" click="video.pause();"/>
            <mx:HSlider liveDragging="true" id="scrubber" minimum="0" maximum="11.5" change="scrubber_changeHandler(event)" thumbPress="video.pause();"/>
        </mx:ControlBar>
    </mx:Panel>

</mx:Application>
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,如果您足够快地单击播放/暂停,您可以更精细地暂停各个帧,但这不是一个好的用户体验

有什么我缺少以编程方式执行此操作吗?

我还要说间隔根据视频长度而变化,3分钟长的间隔有10秒的间隔

apache-flex flash flvplayback actionscript-3 videodisplay

3
推荐指数
1
解决办法
3503
查看次数

如何捕获popen4找不到的命令

我正在使用popen4来捕获stdout,stderr和命令行的退出状态.只要我能抓住上述三件事,我就不会受到popen4的束缚.目前我还没有找到捕获命令未找到错误的好方法.我想which cmd在一个预先任务中做一个,但希望内置一些东西.

在下面你可以运行一个好的任务,糟糕的任务和一个虚假的任务来查看差异.我正在以新鲜rails new apppopen4宝石做这件事

#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require File.expand_path('../config/application', __FILE__)

require 'open4'

# returns exit status 0, all is good
task :convert_good do
  puts "convert good"
  `wget https://www.google.com/images/srpr/logo3w.png`
  status = Open4.popen4("convert logo3w.png output.jpg") do |pid, stdin,stdout,stderr|
    stdin.close
    puts "stdout:"
    stdout.each_line { |line| puts line }
    puts "stderr: #{stderr.inspect}"
    stderr.each_line …
Run Code Online (Sandbox Code Playgroud)

ruby rake ruby-on-rails popen

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

如何在bash中循环jq唯一数组?

我正在尝试遍历唯一名称并提交来自 github json 对象的消息。但是,当数组中有空格时,bash 会将它们视为单独的数组项

#!/usr/bin/env bash

commits='[
  {
    "author": {
      "email": "email@example.com",
      "name": "Chris",
      "username": "chris"
    },
    "committer": {
      "email": "email@example.com",
      "name": "Chris",
      "username": "chris"
    },
    "message": "commit message 1"
  },
  {
    "author": {
      "email": "email@example.com",
      "name": "John",
      "username": "jdoe"
    },
    "committer": {
      "email": "email@example.com",
      "name": "John",
      "username": "jdoe"
    },
    "message": "commit message 2"
  },
    {
    "author": {
      "email": "email@example.com",
      "name": "John",
      "username": "jdoe"
    },
    "committer": {
      "email": "email@example.com",
      "name": "John",
      "username": "jdoe"
    },
    "message": "commit message 3"
  } …
Run Code Online (Sandbox Code Playgroud)

bash jq

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