小编J.M*_*elo的帖子

ActiveRecord Count计算Rails中group by返回的行数

我环顾四周,找不到任何答案.涉及的所有答案都没有使用GROUP BY.

背景: 我有一个paginator,它将为ActiveRecord.find提供选项.它添加了:limit和:offset选项并执行查询.我还需要做的是计算记录总数(减去限制),但有时查询包含:group选项,ActiveRecord.count尝试返回GROUP BY返回的所有行及其每个计数.我在Rails 2.3.5中这样做.

我想要的是ActiveRecord.count返回GROUP BY返回的行数.

下面是一些示例代码,用于演示此实例(用于查找所有标记并按照标记的帖子数量排序):

options = { :select => 'tags.*, COUNT(*) AS post_count',
            :joins => 'INNER JOIN posts_tags',   #Join table for 'posts' and 'tags'
            :group => 'tags.id',
            :order => 'post_count DESC' }

@count = Tag.count(options)

options = options.merge { :offset => (page - 1) * per_page, :limit => per_page }

@items = Tag.find(options)
Run Code Online (Sandbox Code Playgroud)

使用:select选项,Tag.count生成以下SQL:

SELECT count(tags.*, COUNT(*) AS post_count) AS count_tags_all_count_all_as_post_count, tags.id AS tags_id FROM `tags`  INNER JOIN posts_tags  GROUP BY tags.id  ORDER …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails ruby-on-rails-2

19
推荐指数
3
解决办法
3万
查看次数

AS3 Sound.extract()需要帮助

我正在尝试在Flash中创建参数均衡器.我一直在寻找一种方法来读取音频数据,并在Flash动态播放之前弄乱样本.在一个Sound对象中加载声音并使用Sound.extract()读取数据,处理它,然后播放第二个空的Sound对象并将数据写入其sampleData事件似乎是这样做的方法(请更正我如果我错了或有更好的方法).

有没有办法在Sound对象仍在加载声音文件时使用Sound.extract()?我不想在播放之前等待整个声音文件加载.不幸的是,每当我在Sound对象仍然加载时使用Sound.extract()时,它返回一个零长度的字节数组.

有没有办法在播放前等待足够的样本加载?我想,当Flash影片在声音文件仍在加载时通​​过所有加载的样本时,我会再次遇到同样的问题.

这是我的代码的简化版本.它到目前为止工作,但只有当我等待Sound对象触发Event.COMPLETE事件时.

var inputSound:Sound = new Sound();
inputSound.load("somefile.mp3");
inputSound.addEventListener(Event.COMPLETE, loadComplete);

var outputSound:Sound = new Sound();
outputSound.addEventListener(SampleDataEvent.SAMPLE_DATA, processSamples);
var sc:SoundChannel;

/*if I called ouputSound.play() right now, it wouldn't work.*/

function loadComplete(e:Event) : void
{
    sc = outputSound.play();
}

function processSamples(e:SampleDataEvent) : void
{
    var samples:ByteArray = new ByteArray();
    var len:int = snd.extract(samples, 8192);
    var sample:Number;
    var i:int = 0;

    trace(len.toString());

    samples.position = 0;

    //TODO: Sound Processing here

    //The following code plays a sine wave over the input sound as …
Run Code Online (Sandbox Code Playgroud)

flash actionscript-3 flash-cs4

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