Getting ODBC connection results as ruby object - ruby-on-rails

I've had to add an ODBC connection to a remote mysql server into my app.
This is the first time I've done something like this.
I was expecting the results to return as a ruby object, just like the rest of the ActiveRecord requests.
However, I'm beginning to think that maybe ODBC isn't handled the same way through the ruby-odbc gem.
When I output a debug statement on my models which connect through a standard connection, I get the standard
--- !ruby/object:ModelName
attributes:
etc.etc.
but when I output a debug statement on the model connected through the ODBC, i get
[#<ModelName modelID: 1, name: "name" etc. etc. ]
Is there any way for me to get ODBC connections working like the rest of my models?
UPDATE
To clarify, I am hoping to get the data as an object so I can get the data like
<%= #something.name %>
With the way I'm getting the data back now, I can't refer to it as an object.

It looks like it's returning an array of objects, instead of just one. If you only want a single object, just add .first to the result.

Related

Not showing data with react.rb

I'm just trying to use ReactRB with reactive-record.
So the deal is in render part I think. When I'm setting param :user, type: User in React Component class, I can't see any data in my table. Of course Model User in public folder, as this requirement in ReactRB.
Well, in console I see that server is fetching nothing, but right data returned.
What I'm missing? Thanks for the help!
The key for answer is in this screenshot
The details are that the data comes back from the server as a json blob
reactive-record decodes it, but counts on the fact that if you try to json parse a simple string, it raises an error.
opal 0.10 no longer raises standard error, so the whole thing just hangs up.
Just thinking about this... there is a known problem in Opal https://github.com/opal/opal/issues/1545 and this causes a problem in reactive-record. Please make sure that you are not using opal 0.10
One thing to keep in mind is that reactive-record lazy loads records, and attributes. So unless someplace in your render, you access a particular record/attribute that attribute will not show up on the client.
Its hard to tell more without a bit more of your code posted, but here is some help:
Lets say your component looks like this:
class Foo < React::Component::Base
param :user, type: User
def render
"user.name = #{user.name}"
end
end
and someplace either in a controller or in a layout you do this:
render_component '::Foo', {user: User.first}
You might try something very simple like this, just to get familiar with how things work.
What happens should be this: You will render your view and a placeholder for the first User will be sent to the component, during rendering the component looks for that user's name attribute, which it does not have, so that is queued up to fetch from the server. Rendering will complete, and eventually the data will come down from the server, the local model's data will be updated, and components displaying that data will be rerendered.
During prerendering all the above happens internal to the server, and when the component has been rendered the final html is delivered along with all the model data that was used in rendering the component. So on first load if all is working you will not see any fetches from the server.
So if you try out the above small example, and then go into your javascript console you can say things like this:
Opal.User.$first()
and you will see the underlying model data structure returned (I am translating from JS into ruby above... ruby methods all start with $)
You can then do this:
Opal.User.$first().$name()
And you can even do this (assuming there are at least 2 user models):
Opal.User.$find(2).$name()
You should have something like "DummyValue" returned, but then there will be a server fetch cycle in the console, then if you repeat the above command you will get back the actual value!!!
This may not be the best forum for more details, if you need to drop by https://gitter.im/reactrb/chat for more help

Prepared statements in ruby/rails with postgres

I want to execute a rather nasty recursive update query in rails. This means I want to write some raw postgres sql, with parameters, and execute it inside a rails controller.
How do I do that? I can't find a PreparedStatement class in activerecord, there don't seem to be any methods named 'native', I have tried ActiveRecord::Base.connection.exec_delete, I have looked through the source - just cannot, cannot work it out.
I've looked everywhere - the documentation goes in circles.
How would I tell postgres to execute
delete from foo where foo.bar=?
bind 'baz' to the q-mark, and do it without using the active record objects, finders, you-beaut subset thingies and all the rest.
I just want to execute a prepared statement with some bindings. How hard can it be?
(PS, and no: I don't want to jam the parameters into the string myself and execute it as unparameterised sql. It's wrong and it means I have to worry about sanitising the data.)
See the discussion of PreparedStatements in Rails ('Using Prepared Statements') here - http://blog.daniel-azuma.com/archives/216 . Shows you which methods to call, and how to format your arguments.
UPDATE:
Paraphrased from the post:
For the delete method arguments use the template first, followed by a query name (which can be nil) and then an array of values to inject into the statement. So like this:
row_count = connection.delete("DELETE FROM foo WHERE foo.bar=$1", nil, [[nil, 'baz']])

Find the list of list of indexed keys in mongomapper

I am working on a rails app with mongodb and mongomapper. I would like to index few keys in the database. This is first project for me on mongo.
The keys i want to index i can specify like this
User.ensure_index(:email)
as described here
My question is, do i need to call this manually(may be wrapping it in a method) to make the indexing really happening?
And how can i find the list of keys which are having indices?
Here are the answers to my questions
Do i need to call this manually(may be wrapping it in a method) to make the indexing really happening?
Yes, we have to manually call the ensure_index method on Model. So we can wrap that in a method and can call from the console or even a rake task.
def self.create_index
self.ensure_index(:email)
self.ensure_index(:first_name)
self.ensure_index(:last_name)
true
end
then from console
User.create_index
you can check what keys are indexed using mongo's getIndexes() method
like this
mongo #=> enter the mongo console
show dbs #=> see the list of available dbs
use my_database #=> switch to your database
db.table_name.getIndexes() #=> replace table_name with your's
and that's it, you can see the list of indices on your table
Thanks!

How to avoids to hit the database when `update_attribute` is executed with "no changed" data?

I am using Ruby on Rails 3.2.2 and I would like to know if it is possible to use something like caching or IdentityMapping techniques or, at least, "protect" the database from hitting (for example, if the current object data isn't changed the database should not be hit at all) when running the ActiveRecord::Persistence#update_attribute method multiple times in the same controller action and view rendering work flows.
object.update_attribute(foo, bar) if object.changed?
For more infos...
http://ar.rubyonrails.org/classes/ActiveRecord/Dirty.html
hope this helps,...

How can I see the details about the object in server log?

I am trying to debug the user object created by writing ruby code like
puts user
which then I can check it on the server log.
Apparently, the server log says something like
#<User:0x3b53440>
but it does not show details about the user object. (for example, its name or email values)
How should I modify the code so that the detail information about object will be produced?
I want some function in ruby that does similar job as PHP's print_r or var_dump.
Try using the Object.inspect method:
puts user.inspect
Here's the documentation: http://www.ruby-doc.org/core/classes/Object.html#M001025
Often I'll write my own inspect or to_s method for an object, to provide me the view into the object that I want.
If Ruby can't find either of those methods for an object, it'll return the object's ID, and nothing more, because it doesn't know what else to do.

Resources