Rails - 按模型中的商店属性搜索

时间:2015-06-27 21:09:43

标签: mysql configuration store

我如何使用:token和:external_code

进行搜索
class Product < ActiveRecord::Base
    belongs_to :admin
    has_many :lessons
    store :config, accessors: [:token, :external_code]

    has_attached_file :logo, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
    validates_attachment_content_type :logo, :content_type => /\Aimage\/.*\Z/

end

我试过

prod = Product.find_by({
        token: callback_params[:token],
        external_code: callback_params[:code]
     })

但......这个错误

Mysql2::Error: Unknown column 'products.token' in 'where clause': SELECT `products`.* FROM `products` WHERE `products`.`token` = 'Q1LhyCrBsYqt57tRVUgnjmHbyZ0zf81110916' AND `products`.`external_code` = '320552' LIMIT 1

2 个答案:

答案 0 :(得分:1)

您必须先为商店格式定义一个

int getMonthNum(char * name){
    char *months[12] ={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
//char *pointertoarray = &months;
    int i;
        for(i = 0; i < 12; i++){
            if( strcmp(months[i], name)==0){
                return i;
            }   
        }   
    return i;
    }   

为了进行搜索,我在Rails API中找不到任何内容。由于在商店中使用搜索是不合适的,我使用SQL自己的资源来进行查询

store :config, accessors: [:token, :external_code], coder: JSON

允许将它变成一个通用方法,并放入一些抽象类,但大致就是它

答案 1 :(得分:0)

Product.where("token = ?", callback_params[:token].to_yaml, callback_params[:code].to_yaml)

阅读本文以获取更多信息: Searching serialized data, using active record