I want to as that what will happen if I delete the models in the rails project and apply my SQL query, is it necessary to have a model to write Raw SQL queries.
2 Answers
is it necessary to have a model to write Raw SQL queries?
No, if you're using ActiveRecord, then you can use the ActiveRecord::Base.connection.execute and/or ActiveRecord::Base.connection.exec_query methods.
Similarly you can even run prepared statements relying on the ActiveRecord::Base.connection.raw_connection.prepare method that it inherits from your adapter.
4 Comments
Providing you have run a migration and the table is present in your database, it is not necessary to have the model file present locally if you want to run raw sql queries.
In doing this you will miss out on a lot of the perks ActiveRecord/Ruby objects provide and make the process much more error prone and vulnerable to sql injection attacks.