1

I have a Postgres database on my server. This db has a bunch of tables, but the logic of everything is built up in functions on this same database; I have a website in php that calls these functions and these functions make all the validation of fields..

My Question is:

How do I get to do the same thing in Ruby on Rails, I wanted to create a project where I can instead of working directly with the tables in the DB I wanted to call the function in the database that works with these tables?

Passing values from ruby on rails to the functions in the server for them to do the validation.

1 Answer 1

4

You can execute straight SQL with the execute method on the connection:

ActiveRecord::Base.connection.execute("your sql function here")

That will return a PG::Result that you can call each on. The entries will just be Hashes representing the records.

Edit:

Without knowing what your SQL functions look like I can only offer a generic example:

records = ActiveRecord::Base.connection.execute("SELECT * FROM users")
records.each do |record|
  record # => { email: '[email protected]', ... }
end

If you know your SQL query just pass it in to the execute method.

Some further reading:

3
  • Do you have any example i can guide myself? Thank you for answering so fast! Feb 7, 2014 at 19:27
  • im asking for a full example...simple but that gets it done....i want to login users and register them and i have two functions in my dB login and register that do these validations, i want to give these functions values from my ror app so they can validate them. Thank you! Feb 7, 2014 at 19:29
  • I can only give a generic example, edited the answer Feb 7, 2014 at 19:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.