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.
Related
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
I have the following:
def toggle_follow_user tmp_id
user=User.find(tmp_id)
but when I run specs I get the following warning:
DEPRECATION WARNING: You are passing an instance of ActiveRecord::Base to `find`. Please pass the id of the object by calling `.id`.
what is it trying to tell me and how do I fix it?
Just for the sake of a test... I'm not sure what you are passing in that gets assigned to the tmp_id parameter, but I would surmise its an active record object, can you try this?
def toggle_follow_user tmp_id
user=User.find(tmp_id.id)
end
If that works, its because whatever you were sending into toggle_follow_user was an entire active record "row" where Find wants an integer now (representing the ID... it used to pull it out natively, but that is going away it seems)
You're calling toggle_follow_user(a_user), not toggle_follow_user(a_users_id). Try this:
def toggle_follow_user(user)
# just delete the line where you ::find the user, you're already passing it in
This will fix this particular issue - but check that it doesn't break anything else in your code!
If you are wanting to pass the user's id and find it inside the method, then look a call or two down your stack trace, because it's the whole object that's being passed in.
I am using a google drive api, where I am fetching a file object from the google drive api. Now that file may/may not have the downloadLink and the webContentLink depending on the user authenticating the file. Now I have these methods where
download_links[index][1] = i.webContentLink if i.responds_to? :webContentLink
Now it says responds_to is not defined for i.
Now i is a remote object, so i cannot change its implementation and add method_missing or responds_to method. so how do i check if i.webContentLink is a valid method or not in that ith object.
There are two things, here.
First, #responds_to? indeed does not exist, anywhere :) The correct method name for what you want to use is #respond_to?.
Secondly, this is not a ruby method as you ask for it, but I mention it since you post your question with ruby on rails tag. ActiveSupport, from rails, implements a #try method to do that.
It will return the result of the method if it exists, or nil :
download_links[index][1] = i.try( :webContentLink )
Be sure not to abuse that, as it quickly leads to hard to understand code (at some point, you will want to learn about NullObjects).
I'm executing an Active Record query in controller. And now I want to view all of its contents weather it is in the form of array or object. I want to see the structure in which the data is being returned. I'm new to ruby on rails. In PHP we use var_dump() or print_r().
There are a couple of ways to do this. If you want to play with the result interactively, open the rails console by typing rails console. Run the query you want in the console
query_result = MyModel.find_by_interesting_parameter( 'Foo' )
Then use the to_yaml method to dump a nice structure out
puts query_result.to_yaml
Sometimes, it's just easier to see what the view has had back. To do this, use the debug method in the view itself...
<%= debug #post %>
See this page here for more information
Try the to_yaml and the inspect methods.
Take a look at this doc, 'Debugging Rails Applications', particularly section 3, 'Debugging with ruby-debug':
http://guides.rubyonrails.org/debugging_rails_applications.html
I have a Gem that deals with images that get modified. I want to modify it to update the old image but, I need to be able to get the object id.
Right now it uses the following code:
def respond_to?(method,*args, &block)
puts ("++++METHOD #{method.to_s} ARGS #{args} BLOCK #{block}")
args.each do |value|
puts ("ARGS #{value}")
end
but I don't know how to get id from something I pass in nor do I know how to pass the id in, I've tried
#asset.image.s_245_245(:asset_id=>#asset.id) with no success. Args returns nothing. What am I doing wrong?
Update: I am currently reading http://www.simonecarletti.com/blog/2009/09/inside-ruby-on-rails-extract_options-from-arrays/
Update: This too returned blank.
Your question is very unclear. How is the method respond_to? related to your problem with object id?
I am guessing that in reality you wanted to override method_missing, because now you do not call respond_to?, or it is not shown in your examples.
If you have not defined such method, calling image.s_245_245 will trigger method_missing (in the image object) with the parameters you used for respond_to?.
There is a rule, which says that if you use method_missing to handle some calls, then you should also modify respond_to?, and make it returning true when asked for the methods handled by method_missing.
As for object ID, there are two possibilities:
Every object in ruby responds to .object_id (which returns an internal identifier of every object)
ActiveRecord objects respond to .id (which is a primary key in the database).
This is just a side-note, because I suppose that if you start experimenting with method_missing instead of respond_to? you will know which one you want.