Call Database function in server with ruby on rails [closed] - ruby

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
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.

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: 'mail#mail.com', ... }
end
If you know your SQL query just pass it in to the execute method.
Some further reading:
http://www.fngtps.com/2008/free-result-after-using-activerecord-base-connection-execute/
http://blog.daniel-azuma.com/archives/216
http://apidock.com/rails/ActiveRecord/Base/connection

Related

Cannot edit the values of Laravel Collection [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have fetched the data from the database, but I need to make changes to that collection or arrays of collection. Whenever I made changes; these remain limited to the scope of function, so, when the function called again that collection reset to their previous values.
Is there any way to change/edit the laravel collection (the root instance without copying the data like with map(), each())?
To modify the original Collection, you'll want to use transform.
From the docs:
Unlike most other collection methods, transform modifies the collection itself. If you wish to create a new collection instead, use the map method
In most cases I find that map or each is really the appropriate method, but I find this method is especially useful when needing to modify paginated results from a query.

Table in Spring [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
now I teaching Spring, and create simple project.
In my program I have User and Ore(Iron, Coal) but i think in future i add new ore or same goods.
And I don't know how implement many column.
If I create 50 different resources, these are 50 fields in the User class, but this is very inconvenient, how can I create such a relationship in the database. So that many resources can be added quickly.
I thought about design patterns, but I don't know which one is better to choose.
The project is built on Spring, PostgreSQL, MVC
You are misunderstanding the concept of a database. You don't add a column for each entry. Instead, look at your items, ores in this case, what do they have in common?
Could be something like
Id, OreName
Or could be somwthing complex like:
Id, OreName, price + a bunch of other columns with relations
It all depends on your requirements. I highly recommend you check out a database design video. Those would help you tremendously.

Newbie on webapi odata [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Vendor asks to expose a rest api as odata, trying to understand how it works in odata world, my assumption is the data model present themselves as entities, which then enables filter by query string, but how would a odata query work on a relational data model, e.g. on customer orders, I cannot simply join the entire customer and order table then return the entire dataset! (or maybe I could just return them as paged data?) could someone point out some best practice for designing odata webapis? much appreciated!! thanks!!!
I guess, you are using Entity Framework as a Model for OData.
so
return Ok(myDb.Customers)
would do the trick for a multiple row fetch.
Basically, $expand (JOIN on other tables) and $filter and $select are automagically applied using your whole EF model.
But if you fear that private data or secure data would be exposed, you have to apply prefiltering.
return Ok(myDb.Customers.Where(w => w.CustomerId == myCustomerId))
Odata has built in settings to avoid it to return say, 10.000 rows.
You are quite free configuring that all. But expect some learning curve because after all, you might need extra modelling and navigation property optimization.
In addition, you have by default on an OData controller, GET/POST/PUT/PATCH/DELETE methods, but you also might want custom functions/actions that you have to define.
On Github, Microsoft has quite some samples.
https://github.com/OData/ODataSamples/tree/master/WebApi/v4

Is there any way to call trigger explicity in oracle? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Can trigger can be explicitly called ?
I got question like
Trigger can be
a)Implicit
b)Explicit
c)Both
Is there any way to call Trigger Externally ?
From the first paragraph of the overview of triggers (emphasis added):
Like a stored procedure, a trigger is a named PL/SQL unit that is stored in the database and can be invoked repeatedly. Unlike a stored procedure, you can enable and disable a trigger, but you cannot explicitly invoke it.
So no, a trigger cannot be explicitly called.

How to access a table in linq [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to clone a table of a database which exists in a different servers.
e.g.On server A there is a database called EmployeeDataBase which has a table t1 which I need to copy to the database called EmployeeDataBase which exists on Server B.
How to do it using linq query.
My application uses linq to entity.
Thanks
If the tables have the same definition you can use the same mappings, just you need to create different contexts using the appropriate connection strings.
var ctxSource = new EmployeeDataBaseContext("[source connection]");
var ctxDestination = new EmployeeDataBaseContext("[destination connection]");
ctxDestination.t1.InsertAllOnSubmit(ctxSource.t1.ToList());
ctxDestination.SubmitChanges();
Check here the constructors you need.
Entity Framework is not the right technology for this kind of problem. You will be better off using raw ADO.Net. Maybe SqlBulkCopy can be of use: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx

Resources