0

I am trying to run the following test for one of my models:

let(:customer) { create :customer }
let(:ticket) { create :ticket, reserved_by: customer }
let(:order) { Order.new(customer: customer, ticket_order_tickets: [ticket]) }

it 'creates the ticket order with the correct ticket' do
  expect(order.ticket_orders.first.ticket).to be ticket
end

With this code in my model to assign the ticket_order_tickets:

def ticket_order_tickets=(tickets_for_ticket_orders)
  tickets_for_ticket_orders.each do |ticket|
    ticket_orders.build(ticket) if ticket.reserved_by?(customer)
  end
end

But when I run the test, I get the following error:

Failure/Error: let(:order) { Order.new(customer: customer, ticket_order_tickets: tickets) }
     ArgumentError:
       When assigning attributes, you must pass a hash as an argument.

1 Answer 1

1

The each block in ticket_order_tickets is calling build on the ticket_orders relation passing in whatever's in tickets_for_ticket_orders. In your setup, it's single item array containing an actual Ticket, and build (like create) expects a Hash of attributes. That seems to be the issue.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.