小编Ben*_*ton的帖子

使用Devise进行API身份验证(Ruby on Rails)

我正在尝试使用设计配置:token_authenticatable在我的项目中通过json添加身份验证.

我有工作的sessions_controller #create code来自本文 - http://blog.codebykat.com/2012/07/23/remote-api-authentication-with-rails-3-using-activeresource-and-devise /

def create
    build_resource
    resource = User.find_for_database_authentication(:email => params[:email])
    return invalid_login_attempt unless resource

    if resource.valid_password?(params[:password])
        resource.ensure_authentication_token!  #make sure the user has a token generated
        render :json => { :authentication_token => resource.authentication_token, :user_id => resource.id }, :status => :created
        return
    end
end

def invalid_login_attempt
    warden.custom_failure!
    render :json => { :errors => ["Invalid email or password."] },  :success => false, :status => :unauthorized
end
Run Code Online (Sandbox Code Playgroud)

问题是本机设计sessions_controller #crera看起来像这样

  def create
    self.resource = warden.authenticate!(auth_options)
    set_flash_message(:notice, :signed_in) if is_navigational_format?
    sign_in(resource_name, …
Run Code Online (Sandbox Code Playgroud)

authentication api ruby-on-rails devise

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

优化foreach数千项

我在一组25,000个结果中运行下面的代码.我需要优化它,因为我达到了内存限制.

$oldproducts = Oldproduct::model()->findAll(); /*(here i have 25,000 results)*/

foreach($oldproducts as $oldproduct) :
    $criteria = new CDbCriteria;
    $criteria->compare('`someid`', $oldproduct->someid);
    $finds = Newproduct::model()->findAll($criteria);

    if (empty($finds)) {
        $new = new Newproduct;
        $new->someid = $oldproduct->someid;
        $new->save();
    } else {
        foreach($finds as $find) :
            if ($find->price != $oldproduct->price) {
                $find->attributes=array('price' => $oldproduct->price);
                $find->save();
            }
        endforeach;
    }
endforeach;
Run Code Online (Sandbox Code Playgroud)

代码通过someid比较两个表的.如果发现巧合则更新价格列,如果没有创建新记录.

php memory optimization foreach yii

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

标签 统计

api ×1

authentication ×1

devise ×1

foreach ×1

memory ×1

optimization ×1

php ×1

ruby-on-rails ×1

yii ×1