Monofu

u/Monofu






r/rails•

Suppose, we have multiple APIs that returns a JSON response. Both APIs correspond to the same ActiveRecord model(s).

For example, given the JSON response A:

{ 
   'model': 'Volvo',
   'wheels': 4,
   'drivers': ['John', 'Mary', 'Kelly'],
   ....
}

and the JSON response B:

{ 
   'producer': 'Volvo',
   'size': 4,
   'names': ['John', 'Mary', 'Kelly'],
   ....
}

and lastly, the models: Car(id, make, wheels), Driver(id, name), CarDriver(car_id, driver_id)


What would be the most idiomatic-Railsy way to go about implementing serialization and deserialization between the Car model and the various JSON responses? In my real use-case,

42

r/cpp_questions•

Hi, I'm in the process of learning C++ and I'm a little confused about two things. Hoping the reddit community can help me out.

 

i. When to use smart pointers vs references: Is it the general "rule of thumb" to just use references until you can't anymore? Source. Or is it more preferred to pass objects by (smart) pointer?

 

ii. How to return objects from functions (i.e. created on the stack)? Is passing a return parameter frowned upon? Or, is it better to just return by copy and let the compiler optimize?

 

Thank you very much!

89