0

私は次のモデルを持っています:

class Merchant
  acts_as_authentic
  has_one :store
  accepts_nested_attributes_for :store
end

class Store
  belongs_to :merchant
end

Twitter認証にauthlogic_oauthgemを使用しています。登録中に、マーチャントとストアモデルを保存します。oauth認証を無効にすると、両方のモデルが保存されます。oauth認証を有効にすると、マーチャントインスタンスのみが保存されます。

authlogic_oauth gemコードをしばらく見てから、原因を見つけたと思います。authlogic_oauth gemは、oauth呼び出し中にActiveRecord属性をセッションに保存します。ただし、関連付けの属性は保存されません。

# authlogic_oauth : lib/authlogic_oauth/acts_as_authentic.rb
def save(perform_validation = true, &block)
  if perform_validation && block_given? && redirecting_to_oauth_server?
    # My comment: Any nested attributes are not saved in the session
    session_class.controller.session[:authlogic_oauth_attributes] = attributes.reject!{|k, v| v.blank?}
    # some code
  end
  # some code
end

gemコードをハックすることはできますが、もっと良い解決策があるかどうか疑問に思っています。

4

1 に答える 1

0

Oauth呼び出しの間、セッションにストア属性を一時的に保存することで問題に対処しました。この問題に対処するためのより良い方法があることを願っています。

class MerchantsController < ApplicationController
 before_filter :init_nested_attr

 def create
   @merchant = Merchant.new(params[:merchant])
   @merhcant.save do |result|
     #some code
   end
 end

private
 def init_nested_attr
   if session[:authlogic_oauth_attributes]
     params[:merchant] = session[:authlogic_oauth_attributes]
     params[:merchant][:store_attributes] = session.delete(:authlogic_oauth_store_attributes)
   else
     session[:authlogic_oauth_store_attributes] = params[:merchant][:store_attributes]
   end
 end
end
于 2010-01-28T22:36:39.733 に答える