Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Justin Carlson
12,755 PointsRuby on rails query/update external MySQL database
Hello all, I have finished the Ruby on Rails classes and they were fantastic! I have already cranked out 3 sites to automate daily tasks around here. REALLY loving ruby and rails :-).
I have a need to be able to query and update an external MySQL database that has loads of historical data and configuration data for our systems. the goal is to make a control panel for our techs to be able to do complex items quickly and without having to do it manually also to have a self service portal for users so we dont have to do it ALL lol.
Please any direction would be appreciated I have looked around and not found anything too helpfull. I have MySQL in rails and connect to the MySQL server but I DONT want rake/migrates to destroy/touch the historical/config databases.
3 Answers
Alex Alex
Courses Plus Student 2,176 PointsFirst install the mysql gem, and in the config/database.yml you will find three categories and it should be pretty obvious what each do. So just fill those rows with info about your host, username, password...
Alex Alex
Courses Plus Student 2,176 PointsWell, if you have the db on your local machine or on a server, you can connect to the server and use "mysql -u your username -p". Press enter and type your password, type create a database, or use a existing one, and after that, create your tables manually if you don't want to create them with rake. And about naming you tables, name them with the rails convention in mind. I am not an expert with rails and mysql, but it should work similar with sqlite.
Justin Carlson
12,755 PointsAlex,
The external DB's have data that is populated via other applications. They are already there populated and used in production. I need a way for my rails app to have its own db for users/config/etc... and then ALSO manipulate data in 2 other mysql databases (update a row here, delete one there....). I have the mysql rails db working just fine, just looking for a way to integrate it into some of the individual production apps that don't talk to each other.
Jason Seifer
Treehouse Guest TeacherHey Justin Carlson this can be pretty complicated. Essentially you need to add the databases to your config/database.yml and then connect to it from the models themselves. Check out this post for more information.
Justin Carlson
12,755 PointsJason, You sir are a rock star. I searched but did not find this? what vernacular did you use to search for this?. This is EXACTLY what I needed thank you! I would love to see some more Ruby/Rails projects. I first used ruby/rails after signing up and wish I would have found it sooner I just love the way it flows together and is coded!
Justin Carlson
12,755 PointsJustin Carlson
12,755 PointsAlex, Thank you for responding but I have it this far. How do I tell ruby about the database layout? the table names are not anywhere close to the ruby/rails convensions so I assume somewhere I would have to "explain" to rails how the DB is laid out.
Also would adding it in the database file mean a rake command could remove/modify it. I do NOT want rake to EVER touch this database(s).