{"id":6104,"date":"2014-04-13T02:23:51","date_gmt":"2014-04-13T02:23:51","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/rails-3-devise-current_user-is-not-accessible-in-a-model-collection-of-common-programming-errors\/"},"modified":"2014-04-13T02:23:51","modified_gmt":"2014-04-13T02:23:51","slug":"rails-3-devise-current_user-is-not-accessible-in-a-model-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/rails-3-devise-current_user-is-not-accessible-in-a-model-collection-of-common-programming-errors\/","title":{"rendered":"Rails 3 devise, current_user is not accessible in a Model?-Collection of common programming errors"},"content":{"rendered":"<p>Ryan Bates lays out a pretty safe way to implement this kind of strategy in this railscast<\/p>\n<p>This a paid episode (don&#8217;t down vote me!) but you can browse the source code for free<\/p>\n<p>Here he creates a <code>current_tenant<\/code> method, but you could easily substitute <code>current_user<\/code> instead.<\/p>\n<p>Here are the key bits of code&#8230;<\/p>\n<pre><code>#application_controller.rb\naround_filter :scope_current_tenant\n\nprivate\n\ndef current_tenant\n  Tenant.find_by_subdomain! request.subdomain\nend\nhelper_method :current_tenant\n\ndef scope_current_tenant\n  Tenant.current_id = current_tenant.id\n  yield\nensure\n  Tenant.current_id = nil\nend\n\n#models\/tenant.rb\n\ndef self.current_id=(id)\n  Thread.current[:tenant_id] = id\nend\n\ndef self.current_id\n  Thread.current[:tenant_id]\nend\n<\/code><\/pre>\n<p>Then in the model you can do something like&#8230;<\/p>\n<pre><code>default_scope { where(tenant_id: Tenant.current_id) }\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ryan Bates lays out a pretty safe way to implement this kind of strategy in this railscast This a paid episode (don&#8217;t down vote me!) but you can browse the source code for free Here he creates a current_tenant method, but you could easily substitute current_user instead. Here are the key bits of code&#8230; #application_controller.rb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6104","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6104"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6104\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}