Skip to content

Instantly share code, notes, and snippets.

@hukl
Created October 23, 2011 19:58
Show Gist options
  • Save hukl/1307807 to your computer and use it in GitHub Desktop.
Save hukl/1307807 to your computer and use it in GitHub Desktop.
Rails has_many :through regression
class Order
has_many :order_tickets
has_many :tickets, :through => :order_tickets
end
class Ticket
has_many :order_tickets
has_many :orders, :through => :order_tickets
end
class OrderTicket
belongs_to :order
belongs_to :ticket
end
ticket = Ticket.create :name => "Standard"
order = Order.create
order.tickets = [ ticket, ticket, ticket ]
order.tickets.reload.count => 1
# works for different tickets like :name => Standard // Student // Guset
# so it behaves as if :uniq => true was set.
# Worked in rails 3.0.7-3.0.10 but not in rails 3.1.0
# In rails 3.0.x it would return
# o.reload.tickets => [ <Ticket id: 1 …>, <Ticket id: 1 …>, <Ticket id: 1 …>]
# Now only
# o.reload.tickets => [ <Ticket id: 1 …>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment