adelevie / parse-ruby-client

A simple Ruby client for the parse.com REST API
MIT License
415 stars 137 forks source link

client.query - error client is undefined method in the contoller #215

Closed OpConTech closed 7 years ago

OpConTech commented 7 years ago

I have been using v0.3 for a long time and a typical query like this works great:

      videos_query = Parse::Query.new("Videos").tap do |q|
      #q.eq("UserTagID", current_user.UserTagID)
      q.eq("CompanyTag", current_user.CompanyTag)
      q.eq("videoPrivacy", 'No')
      q.order_by = "-videoCreatedAt"
      q.limit = 1000 
      @results = q.get
      end.get

Now I have migrated to parse server and so have been trying to use the latest version from the master branch so as to be able to specify my Parse Server URI.

The new documentation seems to show replacing Parse::Query.new with client.query. However when I do that it crashes the controller because client is an undefined method.

Any suggestions would be much appreciated,

Tom

rhymes commented 7 years ago

@OpConTech hi, can you show me how you create the client? You have to do something like that:

client = Parse.create ...
query = client.query("Videos").tap do |q|
....
end
OpConTech commented 7 years ago

Hi rhymes,

I don't do that on the line above but I am able to get this to work in an initializer parse-client.rb

---- parse-client.rb -------- ::MyParseClient = Parse.create
:application_id => ENV['PARSE_SERVER_APPLCATION_ID'], :api_key => ENV['PARSE_SERVER_API_KEY'], # optional, defaults to nil .....

Before in v0.3.0 I didn't have to do this the existing Parse::Query.new just worked in the controller videos_query = Parse::Query.new("Videos").tap do |q|

Using the initializer I showed above I have been able to get this to work today: videos_query = MyParseClient.query("Videos").tap do |q|

But is there a better way to do this than what I have above? Or do I have to replace every Parse::Query.new in my code to MyParseClient.query( in order to use the v1.0.0 alpha?

rhymes commented 7 years ago

Yes, the 1.0 breaks compatibility to remove the singleton object that was present in the 0.3.

OpConTech commented 7 years ago

Thanks rhymes. Based on your feedback I have the queries working now.

One more quick question to help me finish the transition from 0.3 to 1.0.0 alpa. I have been defining current_user in the application_controller.rb as:

----------- application_controller.rb------------------ private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id] end // Note User.find is from the old parse-resource gem

But I am having trouble getting this to work correctly in v.1.0.0 alpha because when I try: @current_user ||= Parse::User.find(session[:user_id]) if session[:user_id] the compiler lets make know that Parse::User in v1.0 does not have a .find method.

Any chance you might know a good way to define a global @current_user in v1.0?

rhymes commented 7 years ago

@OpConTech I'm not sure how parse-resource works, I've never looked into it.

You could create a method "find_user_from_parse" or whatever that just uses parse-ruby-client, see how you can retrieve users: https://github.com/adelevie/parse-ruby-client#retrieving-users

Or you could just keep using parse-resource as before, it's not maintained but if it's still working for you why change it?

OpConTech commented 7 years ago

@rhymes I'm still struggling with a way to setup current_user in parse_ruby_client.

With parse_ruby_client after you have logged in using:

user = Parse::User.authenticate("username", "password", client)

how do you create a current_user so that in any controller you can check the status as well as pull data from the fields in the current_user object.

Is there a Parse::User.current_user or something?

Any help would be much appreciated.

rhymes commented 7 years ago

No, because current_user in Rails is part of, for example, the devise Gem. It's an helper method injected in the controller and the view. Definitely not parse-ruby-client's responsibility.

I think you're looking to define a global variable, there are many ways to go about it: http://dalibornasevic.com/posts/9-ruby-singleton-pattern

Know that probably none of those are thread safe (if your app uses multi-thread it might be a concern), this is a thread safe option http://bonamin.org/blog/2012/03/06/thread-safe-global-variables-in-ruby/

Hope this helps, I'm closing this because it's out of the scope of the library.