Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your lib folder add a file with the follwing content:</p> <pre><code>class Class def thread_local_accessor name, options = {} m = Module.new m.module_eval do class_variable_set :"@@#{name}", Hash.new {|h,k| h[k] = options[:default] } end m.module_eval %{ FINALIZER = lambda {|id| @@#{name}.delete id } def #{name} @@#{name}[Thread.current.object_id] end def #{name}=(val) ObjectSpace.define_finalizer Thread.current, FINALIZER unless @@#{name}.has_key? Thread.current.object_id @@#{name}[Thread.current.object_id] = val end } class_eval do include m extend m end end end </code></pre> <p>I found this <a href="http://coderrr.wordpress.com/2008/04/10/lets-stop-polluting-the-threadcurrent-hash/" rel="nofollow noreferrer">here</a></p> <p>Then add code in the controller like this:</p> <pre><code>class ApplicationController &lt; ActionController before_filter :set_subdomain private def set_subdomain User.subdomain = request.subdomains[0] end end </code></pre> <p>And now you can do the following in your user model (assuming your company model has a method called subdomain:</p> <pre><code>class User &lt; ActiveRecord::Base thread_local_accessor :subdomain, :default =&gt; nil def self.find_by_email_within_company(email) self.find_by_email(email) company_subdomains = user.brands.map(&amp;:company).map(&amp;:subdomain) company_subdomains.include?(self.subdomain) &amp;&amp; user end end </code></pre> <p>And FYI:</p> <pre><code>companies = user.brands.map(&amp;:company).map(&amp;:subdomain) </code></pre> <p>is the same as</p> <pre><code>companies = [] user.brands.each do |b| companies &lt;&lt; b.company.subdomain end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload