On Friday, February 28, 2020 at 4:24:30 PM UTC-8, Sunho Kim wrote:
>
> Hi!
>
> When using multi_insert with returning, is there a way to get inserted 
> rows as models instead of hashes?
>

Not directly.  INSERT/UPDATE/DELETE returning always return plain hashes 
and not model instances.
 

> For example, Model.returning.multi_insert(hashes) returns [<Model 
> @values={:id =>123 ....}>] instead of [{id: 123 ....}]
>

This will probably be an easy work around:

  Model.returning.multi_insert(hashes).map{|h| Model.call(h)}
 

> Also, is there a way to pass a hash with a primary key to Model.new(hash) 
> without getting MassAssignmentRestriction?
>

Model.unrestrict_primary_key

However, note that Model.new creates an instance not yet saved to the 
database.  If you want a instance for which there is already a database 
row, use Model.call/Model.load.  However, note that Model.call/Model.load 
do not do any typecasting, they use the provided hash directly as the 
values for the instance.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/5f4a5397-4fbe-4dd9-a479-1bfdd1feabfd%40googlegroups.com.

Reply via email to