Let model Quote have attributes [price, description]
Let model Invoice have attributes [price, description, priority]
Let invoice an object from Model Invoice with attributes {price: 10, description: 'lamp', priority: 10}
invoice = {price: 10, description: 'lamp', priority: 10}
Let’s say I want to copy invoice attributes to a new quote.
quote = Quote.new(invoice.attributes)
This raises an error that priority does not existe in model Quote.
How do I copy invoice attributes to a new quote but only the attributes that a quote can accept?
You can
selectonly the attributes thatQuotehas:As noted by @aceofspades (but not with a dynamic solution), you can use ActiveSupport’s
sliceas well: