0

I have a lot of data in JSON and want to import it to my db. My problem is, that I have several after_create and after_update methods in my controller, where other stuff is written to the db.

So I was thinking to use the controller for importing the data. I can format my data to be the same format as the params hash. But when I create an instance of the controller, setting the params hash and calling create it doesn't find some class variables.

Then I tried to force RSpecs controller test to do the work with a post, but I failed there too. Maybe it's not possible, or there is even a better option to import data using all the hooks in my controllers.

4
  • another thought: Maybe it's easier / faster to only post the actual data to my website. But how to handle the authentication (it's done with devise)? Commented Dec 14, 2012 at 19:21
  • import to db of your json data is one time? or request level.. Commented Dec 14, 2012 at 19:22
  • Have you thought about moving some of that code from your controller to the model? I know that doesn't fix your problem but I would think convention holds "skinny controllers, fat models." Commented Dec 14, 2012 at 19:22
  • It's actual not my code, and yes, it's a little bit messy ;). The import isn't a one time import, new data gets fetched every night, about 1000 products per night. Commented Dec 14, 2012 at 19:24

1 Answer 1

1

You can use something like RestClient to post the data to the website itself.

As far as handling authentication, devise supports HTTP basic authentication but it has to be enabled. You can easily use RestClient with HTTP basic authentication if that is an option. Another option is to use Token Authentication with Devise.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried it with token authentication, that worked! I think I will do it through the controller for now, but will refactor the code step by step ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.