Could someone explain this http://blog.leetsoft.com/articles/2005/10/31/scoped-databases 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. -- Posted via http://www.ruby-forum.com/.
On Feb 18, 2006, at 12:27 PM, James Whittaker wrote:> Could someone explain this > > http://blog.leetsoft.com/articles/2005/10/31/scoped-databases > > 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. >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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060218/d760fd93/attachment.html
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. -- Posted via http://www.ruby-forum.com/.
On Feb 19, 2006, at 6:01 AM, James Whittaker wrote:> 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. >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