Blake Geist
Blake Geist

Reputation: 285

Rails 4 Dynamic Subdomains

Hi I am following the tutorial at http://railscasts.com/episodes/221-subdomains-in-rails-3 and trying to make it work for rails 4. The problem I encountered is in the my controller and with the find_by_subdomain! tag, I have read that most of the the find_by methods where taken out of rails 4, and was wondering what the work around was.

my controller currently looks like

def set_city
  @city = City.friendly.find_by_subdomain!(request.subdomain)
end

and the error I am getting is

undefined method `find_by_subdomain!' 

Also in case it helps my routes currently look like

  get '/' => 'cities#show', :constraints => { :subdomain => /.+/ }

Any help would by greatly appreciated and I would be happy to clarify if needed.

Upvotes: 2

Views: 5805

Answers (1)

tybro0103
tybro0103

Reputation: 49713

The find_by_* method is on the class itself:

City.find_by_subdomain!(request.subdomain)

What is City.friendly returning? Whatever it is, I doubt it's the City class.

Also, you can use find_by with a hash now:

City.find_by subdomain: request.subdomain

http://guides.rubyonrails.org/active_record_querying.html#retrieving-a-single-object

Upvotes: 7

Related Questions