小编Chr*_*yne的帖子

使用will_paginate从Rails查询获取总结果计数

我从我的Rails应用程序中获取了一个对象列表,并像往常一样使用will_paginate页面,然后我有一个方法用于将搜索的详细信息保存到数据库:

per_page=10
session[:search_params] = params[:search_people]
@documents = Person.search_people(params[:search_people], params[:page], per_page)

Search.create(:user_id     => (!current_user ? 0 : current_user.id),
   :search_type => "Person", 
   :firstname   => params[:search_people][:first_name], 
   :lastname    => params[:search_people][:last_name],
   :results     => @documents.count )
Run Code Online (Sandbox Code Playgroud)

问题是,搜索结果的数量(@douments.count)总是<= per_page用于will_paginate.

我明白为什么会这样,但有没有办法绕过它而不运行两次查询,一次使用will_paginate而一次没有?

ruby-on-rails will-paginate

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

计算SQL中不同多列的出现次数

我正在尝试计算用户表中一组不同城市和国家/地区的出现次数.

该表的类似于:

userid  city       country
------  ---------  --------------
1       Cambridge  United Kingdom
2       London     United Kingdom
3       Cambridge  United Kingdom
4       New York   United States
Run Code Online (Sandbox Code Playgroud)

我需要的是每个城市和国家/地区的列表,其中包含出现次数:

Cambridge, United Kingdom, 2
London, United Kingdom, 1
New York, United States, 1
Run Code Online (Sandbox Code Playgroud)

目前我运行SQL查询来获取不同的对:

$array = SELECT DISTINCT city, country FROM usertable
Run Code Online (Sandbox Code Playgroud)

然后在PHP中将其读入数组,并循环遍历数组,运行查询以计算数组中每行的每个匹配项:

SELECT count(*) FROM usertable
WHERE city = $array['city']
AND   country = $array['country']
Run Code Online (Sandbox Code Playgroud)

我假设我对SQL缺乏掌握的东西 - 这样做的正确方法是什么,最好没有PHP的干预?

sql

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

将LibreOffice Calc URL转换为文本

我有一个LibreOffice 3电子表格(在Ubuntu 11.04上),其中包含数百个超链接列,只显示为"链接".

我需要将这些转换为纯文本,或更糟糕的超链接,显示超链接而不是文本'链接'.是否有可以执行此操作的函数,或者可能是在整个电子表格中执行此操作的方法?

libreoffice-calc

10
推荐指数
2
解决办法
9101
查看次数

从WooCommerce中的国家/地区代码获取国家/地区名称

Wordpress中的WooCommerce定义了国家类如下(编辑简洁)

class WC_Countries {
    public $countries;

    public function __construct() {
        global $woocommerce, $states;

        $this->countries = apply_filters( 'woocommerce_countries', array(
            'AF' => __( 'Afghanistan', 'woocommerce' ),
            'AX' => __( '&#197;land Islands', 'woocommerce' ),
            'AL' => __( 'Albania', 'woocommerce' ),
            'DZ' => __( 'Algeria', 'woocommerce' ),

        ));
    }
}
Run Code Online (Sandbox Code Playgroud)

下订单时,国家/地区代码将写入Wordpress wp_postmeta表,并且可以使用以下get_post_meta()函数在任何可以访问订单ID的地方提取:

get_post_meta( $order->id, '_shipping_country', true ),
Run Code Online (Sandbox Code Playgroud)

问题是,由于我们只是从DB中提取两个字符,如何将Shipping国家代码(例如AF)转换为Countries类中给出的Country Name?

php wordpress woocommerce

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

从OpenLayers功能中删除所有弹出窗口

我正在使用OpenLayers创建地图和绘图位置.每个位置都有一个标记和一个弹出窗口,并使用OpenLayers.Feature创建 - 目前,我肯定在我的舒适区之外,所以我将示例代码拼凑在一起.

一个标记创建如下(我为了简洁而切断了我希望明显的变量赋值):

function addMarker(ll, popupClass, popupContentHTML, closeBox, overflow, type)
{
    var feature = new OpenLayers.Feature(markerLayer, ll);  
    feature.closeBox = closeBox;
    feature.popupClass = popupClass;
    feature.data.icon = icon;
    feature.data.popupContentHTML = popupContentHTML;
    feature.data.overflow = (overflow) ? "auto" : "hidden";

    var marker = feature.createMarker();
    var markerClick = function (evt) {
        if (this.popup == null) {
            this.popup = this.createPopup(this.closeBox);
            map.addPopup(this.popup);
            this.popup.show();
        } else {
            this.popup.toggle();
    }
        currentPopup = this.popup;
        OpenLayers.Event.stop(evt);
    };

    marker.events.register("mousedown", feature, markerClick);
    markerLayer.addMarker(marker);
}
Run Code Online (Sandbox Code Playgroud)

地图可以包含许多标记.

单击标记时,弹出窗口会打开和关闭.我想要的是,当点击一个新标记并弹出一个弹出窗口时,将关闭地图上所有标记的所有弹出窗口关闭 - 也就是说,我一次只想显示一个弹出窗口.

可能我的方法都是错误的,但是会感激指针,甚至只是尝试的想法.

javascript openlayers

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

Rails中的find_by_sql,访问生成的数组

我试图在Rails中以非常快速和肮脏的方式运行查询,而不是将模型的其余部分放在适当的位置.我知道这是不好的做法,但我只是需要在紧迫的时间内快速得出结果,直到我得到整个解决方案.

我有基于重量的运费价格的物品.重量存储在物品中,价格存储在表shipping_zone_prices中,我目前所做的就是查找与重量比销售物品重的第一行相关的价格:

class Item < ActiveRecord::Base
  def shipping_price
    item_id = self.id
    shipping_price = ShippingZonePrice.find_by_sql(
      "SELECT z.price as price
       FROM shipping_zone_prices z, items i
       WHERE i.id = '#{item_id}'
       AND z.weight_g > d.weight
       ORDER BY z.weight_g asc limit 1")    
  end
end
Run Code Online (Sandbox Code Playgroud)

这种作品.SQL完成了这项工作,但是当插入应用程序时,如下所示:

 <%= @item.shipping_price %> Shipping
Run Code Online (Sandbox Code Playgroud)

我显示以下内容:

[#<ShippingZonePrice price: 12>] Shipping
Run Code Online (Sandbox Code Playgroud)

在此示例中,"12"是从数据库中提取的价格,并且是正确的.@ item.shipping_price.class返回'Array'.尝试使用[0](或任何其他整数)访问数组会返回空白.

有没有其他方式来访问它,或者我错过了一些基本的东西?

ruby-on-rails

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

Ruby on Rails中的复选框表单基于多对多关系

我的Ruby on Rails应用程序有一个DocumentType模型和一个PersonType模型.文档是DocumentType(例如Letter,Postcard)并列出一个或多个People.为每个Person分配一个PersonType来表示他们与Document(发件人,收件人,作者等)的关系.仅允许用户将People分配给与DocumentType相关的PersonType.

表定义:

create_table "document_types", :force => true do |t|
 t.string   "name" 
end

create_table "document_types_person_types", :id => false, :force => true do |t|
 t.integer "document_type_id"
 t.integer "person_type_id"
end

create_table "person_types", :force => true do |t|
 t.string   "name"
end
Run Code Online (Sandbox Code Playgroud)

型号定义:

class Document < ActiveRecord::Base
 has_many :people, :dependent => :destroy
 belongs_to :document_type
end

class DocumentType < ActiveRecord::Base
 has_many :documents
 has_and_belongs_to_many :person_types
end

class Person < ActiveRecord::Base
belongs_to :document
has_and_belongs_to_many :person_types
end

class PersonType < ActiveRecord::Base
 has_and_belongs_to_many :people
 has_and_belongs_to_many :document_types
end
Run Code Online (Sandbox Code Playgroud)

表格示例: …

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

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

使用jQuery从PHP关联数组中获取值

我有一个从数组生成PHP的html选择元素,如下所示

$countries = array(
    array('iso' => 'AF', 'name' => 'Afghanistan',  'key' => 'Red'),
    array('iso' => 'AX', 'name' => 'Åland Islands','key' => 'Yellow'),
    array('iso' => 'AL', 'name' => 'Albania',      'key' => 'Blue')
);

$select = '<select id="country_select" onchange="countryfind()">';
foreach($countries as $country) {
    $select .= '<option value="'.$country['key'].'">'.$country['name'].'</option>';
}
$select = '</select>';

return $select;
Run Code Online (Sandbox Code Playgroud)

选择框使用小的javascript函数显示所选国家/地区的国家/地区密钥:

function countryfind(){
    jQuery('#countrykey').text(
         jQuery('#country_select option:selected').val()
)};
Run Code Online (Sandbox Code Playgroud)

我现在想使用该键与另一个包含有关国家/地区信息的PHP数组进行交互,并在#countrykey div中显示该国家/地区而不是所选值:

$si_federations = array(
    'Red'   => array('name'=>'A Red Country', 'url'=>'http://www.redcountry.com'),
    'Blue'   => array('name'=>'A Blue Country', 'url'=>'http://www.soroptimisteurope.org'),
);
Run Code Online (Sandbox Code Playgroud)

这是可以轻松实现的,还是我的方法完全错了?

javascript php jquery

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

使 .gitignore 忽略 tmp 目录中的嵌套文件夹

这是我的 .gitignore 文件

.bundle
db/*.sqlite3*
log/*.log
*.log
tmp/*
doc/
*.swp
*~
.project
.DS_Store
log/*
Gemfile.lock
Run Code Online (Sandbox Code Playgroud)

那么为什么它总是说这样的话:

# Changes not staged for commit:
#   modified:   tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af
Run Code Online (Sandbox Code Playgroud)

即为什么它不忽略我的 tmp 目录?由于文件和目录名称似乎是随机的,我不认为我可以显式添加它们。

git

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

在OpenLayers中更改默认标记图标

我试图在OpenLayers中绘制国家/地区位置,并且在更改默认图标时遇到问题.

我的基本代码如下:

    function addCountryMarker(ll, popupClass, popupContentHTML, closeBox, overflow) {
        var feature = new OpenLayers.Feature(alumniCountries, ll); 
        feature.closeBox = closeBox;
        feature.popupClass = popupClass;
        feature.data.popupContentHTML = popupContentHTML;
        feature.data.overflow = (overflow) ? "auto" : "hidden";

        var marker = feature.createMarker();

        var markerClick = function (evt) {
            if (this.popup == null) {
                this.popup = this.createPopup(this.closeBox);
                map.addPopup(this.popup);
                this.popup.show();
            } else {
                this.popup.toggle();
            }
            currentPopup = this.popup;
            OpenLayers.Event.stop(evt);
        };
        marker.events.register("mousedown", feature, markerClick);
        alumniCountries.addMarker(marker);
    }
Run Code Online (Sandbox Code Playgroud)

这一切都运行正常,但显示默认的openLayers图标.以下行更改图标但是它会破坏markerClick函数中的弹出窗口:

feature.icon = new OpenLayers.Icon("marker-blue.png");
Run Code Online (Sandbox Code Playgroud)

使用marker.icon的同一行也会更改图标,但也会中断弹出窗口.关于如何在不破坏弹出窗口的情况下更改图标的任何指示都将非常感激.

openlayers

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