Ruby on Rails: Using table_name_prefix and namespace to organize models

class City < ApplicationRecord
end
City.last
City Load SELECT `cities`.* FROM `cities` ORDER BY `cities`.`id` DESC LIMIT 1
class City < ApplicationRecord
self.table_name = 'tw_cities'
end
City.last
City Load SELECT `tw_cities`.* FROM `tw_cities` ORDER BY `tw_cities`.`id` DESC LIMIT 1
class City < ApplicationRecord
self.table_name = 'tw_cities'
end
class Country < ApplicationRecord
self.table_name = 'tw_countries'
end
class Town< ApplicationRecord
self.table_name = 'tw_towns'
end
# app/models/tw.rbmodule TW
def self.table_name_prefix
'tw_'
end
end
# app/models/tw/city.rbmodule TW
class City < ApplicationRecord
end
end
# app/models/tw/country.rbmodule TW
class City < ApplicationRecord
end
end
# app/models/tw/town.rbmodule TW
class Town< ApplicationRecord
end
end
TW::City.last
TW::City Load SELECT `tw_cities`.* FROM `tw_cities` ORDER BY `tw_cities`.`id` DESC LIMIT 1
TW::Country.last
TW::Country Load SELECT `tw_countries`.* FROM `tw_countries` ORDER BY `tw_countries`.`id` DESC LIMIT 1
# app/models/tw/street.rbmodule TW
class Street< ApplicationRecord
end
end

Reference

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
icelandcheng

icelandcheng

67 Followers

Programming Skill learner and Sharer | Ruby on Rails | Golang | Vue.js | Web Map API