2010-01-28 4 views
0

я следующие модели:Проблема с accepts_nested_attributes_for при использовании authlogic_oauth

class Merchant 
    acts_as_authentic 
    has_one :store 
    accepts_nested_attributes_for :store 
end 

class Store 
    belongs_to :merchant 
end 

Я использую authlogic_oauth камень для аутентификации Twitter. Во время регистрации я сохраняю модель Merchant и Store. Если я отключу аутентификацию oauth, обе модели будут сохранены. Когда я включаю аутентификацию oauth, сохраняется только экземпляр Merchant.

Проведя некоторое время, глядя на код самообслуживания authlogic_oauth, я думаю, нашел виновника. Драйвер authlogic_oauth сохраняет атрибуты ActiveRecord в сеансе во время вызовов oauth. Но он не сохраняет атрибуты ассоциаций.

# 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 

Я могу взломать код драгоценного камня, но мне интересно, есть ли лучшее решение.

ответ

0

Я обратился к проблеме, сохраняя атрибуты Store временно в сеансе на время вызовов 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