2

예 ..."tenant"클래스가 acts_as_tenant gem과 함께 저장되기 전에 연관된 레코드를 작성하는 방법은 무엇입니까?

client이라는 클래스가 있는데, 이는 tenant 모델입니다.
레코드가 생성되어 각각 새로운 client과 함께 저장됩니다. tasks이라고합시다.
나는 다음을 수행 build_defaults라는 메소드를 호출 client의 내부 after_initialize 콜백 ...

def build_defaults 
self.tasks.build(
    Task.new({ 
    :name => 'task 1', 
    :description => 'task 1 desc', 
    :view_module => 'task_1_template' 
    }), 
    Task.new({ 
    :name => 'task 2', 
    :description => 'task 2 desc', 
    :view_module => 'task_2_template' 
    }), 
    Task.new({ 
    :name => 'task 3', 
    :description => 'task 3 desc', 
    :view_module => 'task_3_template' 
    }), 
    Task.new({ 
    :name => 'task 4', 
    :description => 'task 4 desc', 
    :view_module => 'task_4_template' 
    }), 
    Task.new({ 
    :name => 'task 5', 
    :description => 'task 5 desc', 
    :view_module => 'task_5_template' 
    }) 
) 
end 

task 클래스는 내가 뭔가를 제기 @client = new Client(:name => "Test Client") 을 수행하는 이동 acts_as_tenant :client

등의 설치가 만든 ActsAsTenant::Errors::NoTenantSet: ActsAsTenant::Errors::NoTenantSet

acts_as_tenant의 체크가 new_record 인 경우 조건부로 무시하는 방법이 있습니까? 또는이 유형의 것을 처리하는 더 좋은 방법은 무엇입니까?

저는 몇 달 전부터 레일스/루비에 대해 상당히 새로운 ... ...? ActsAsTenant.current_tenant을 무시하는 것이 좋은 경우

는 UPDATE

글쎄, 난 내가 "after_create"로 변경 있는지 생각하고 내가 self.tasks.create! 통화를 할 수있는 방법 내에서 ActsAsTenant.current_tenant = self을 설정 ...하지만 확실하지 생각? 새 세입자를 만든 다음 새로운 클라이언트와 연관된 다수의 레코드를 만들려면

after_create :build_defaults 
    def build_defaults 
    ActsAsTenant.current_tenant = self 
    self.tasks.create!({ 
     :name => 'Facebook > Page Like', 
     :description => 'Request that they like your page.', 
     :view_module => 'facebook_page_like' 
    }) 
    self.tasks.create!({ 
     :name => 'Facebook > Share', 
     :description => 'Share a link on Facebook', 
     :view_module => 'facebook_share_link' 
    }) 
    self.tasks.create!({ 
     :name => 'Twitter > Tweet', 
     :description => 'Post a tweet on a user behalf.', 
     :view_module => 'twitter_tweet' 
    }) 
    self.tasks.create!({ 
     :name => 'Twitter > Follow', 
     :description => 'Follow the company twitter user.', 
     :view_module => 'twitter_follow' 
    }) 
    self.tasks.create!({ 
     :name => 'Giveaway Share', 
     :description => 'This allows you to earn 5 extra entries by re-sharing this giveaway.', 
     :view_module => 'giveaway_share' 
    }) 
    end 

답변

2

, 당신은 먼저 현재 세입자를 설정해야합니다 또는 acts_as_tenant 당신이 그렇게 할 수 없습니다.

블록이 작동

ActsAsTenant.with_tenant(newly_created_tenant) do 
    # Current tenant is set for all code in this block 
end 

의 현재 세입자 설정 :

(또한 저를 읽어 확인) 귀하의 접근 방식은 작동하지만, acts_as_tenant뿐만 아니라 이것에 대한 특정 API가 있습니다.

그러나 계정을 만든 후에 새 테넌트에게 앱에 직접 액세스하기를 원할 경우. 로그인 한 다음 기본값을 만드는 것이 좋습니다. 로그인하는 방법은 인증 솔루션에 따라 다릅니다.

+0

감사합니다. Erwin, 매우 도움이되고 내가 찾고있는 것이 었습니다. 당신이하는 일을 계속하십시오! –