Forum: Ruby on Rails scoped databases need explanation...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B0be4bfef5a22697a0f44f4fe295aaef?d=identicon&s=25 James Whittaker (aftershock)
on 2006-02-18 21:27
Could someone explain this

http://blog.leetsoft.com/articles/2005/10/31/scope...

in a way that can be understood with an example if possible. Tobi Lutke
has started to explain but it does not make any sense.

"Since rails 0.13.1 we support calling class methods over associations."
what?? give an example.

also

"The Shop object is figured out at the beginning of each request by
looking at the incoming domain of the user." has no example of how it
could be used.

I feel this is a very important aspect to rails that needs some futher
explanation.
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-02-18 22:22
(Received via mailing list)
On Feb 18, 2006, at 12:27 PM, James Whittaker wrote:

> what?? give an example.
>
> also
>
> "The Shop object is figured out at the beginning of each request by
> looking at the incoming domain of the user." has no example of how it
> could be used.
>
> I feel this is a very important aspect to rails that needs some futher
> explanation.
>


	What they are probably doing is getting the shop in a before_filter
using the subdomain. Something like this:

class ApplicationController < ActionController::Base

    before_filter :setup_shop

    attr_accessor:shop

    protected

    def setup_shop
	  if @shop = Shop.find_by_subdomain(request.subdomains.first)
      else
	    @shop = Shop.find_by_subdomain("default")
      end
    end
end

This way on every request @shop will be set the the correct shop
according to the subdomain in the url like:

http://fooshop.example.com

So @shop will be the current shop account on every request and if the
request doesn't have a subdomain it gets set tho the default shop.
Then you can scope your queries like in the blog article:

class Product < AR:B
   def self.search(q)
       find(:all, :conditions => "title LIKE '%#{q}%'")
   end
end

@shop.products.search(â??snowboardsâ?) will run this find query:

Product.find(:all, :conditions => ["title LIKE ? AND shop_id = ?", "%#
{q}%", 2]

The shop_id = 2 is added automatically by these scoped queries. So in
the above @shop.id would be 2 and the Product search is scoped to
that particular shop.

Hope that makes sense to you ;)

Cheers-

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
B0be4bfef5a22697a0f44f4fe295aaef?d=identicon&s=25 James Whittaker (aftershock)
on 2006-02-19 15:01
That is brilliant and now makes sense to me thanks.

Just one thing, how can I set the subdomains up for each new account on
the system. Is that done within rails or is it a server setup?

Basically I have a table called "customers" and for each new customer I
would like to map their name to a subdomain just like the other hosted
apps out there.
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-02-19 21:44
(Received via mailing list)
On Feb 19, 2006, at 6:01 AM, James Whittaker wrote:

>
James-

	Well you need to configure your web server and dns to accept any
subdomain. And then you can just store the customers subdomain in the
Customer model and use the technique I just showed you. There are
also some good helpers for using subdomains and redirecting to them
here:

module AccountLocation
   def self.included(controller)
     controller.helper_method
(:account_domain, :account_host, :account_url)
   end

   protected
     def default_account_subdomain
       @account.company_abbrev if @account && @account.respond_to?
(:company_abbrev)
     end

     def account_url(account_subdomain = default_account_subdomain,
use_ssl = request.ssl?)
       (use_ssl ? "https://" : "http://") + account_host
(account_subdomain)
     end

     def account_host(account_subdomain = default_account_subdomain)
       account_host = ""
       account_host << account_subdomain + "."
       account_host << account_domain
     end

     def account_domain
       account_domain = ""
       account_domain << request.subdomains[1..-1].join(".") + "." if
request.subdomains.size > 1
       account_domain << request.domain + request.port_string
     end
end

	Inlcude that in your application controller like so:

class ApplicationController < ActionController::Base
   include AccountLocation


Cheers-

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
This topic is locked and can not be replied to.