Rails: Copying attributes from an object to another using the “attributes” method

前端 未结 4 1745
野趣味
野趣味 2020-12-14 18:26

Let model Quote have attributes [price, description]

Let model Invoice have attributes [price, description, priority]

4条回答
  •  时光说笑
    2020-12-14 18:55

    How about the slice method from ActiveSupport?

    quote = Quote.new(invoice.attributes.slice(:price, :description))
    

    or even

    quote = Quote.new(invoice.attributes.slice(*Quote.accessible_attributes))
    

提交回复
热议问题