The following model works fine for reads, updates, and deletes but not for creating.
class Space < ActiveRecord::Base
has_many :space_prices, :dependent => :destroy
accepts_nested_attributes_for :space_prices, :allow_destroy => true
has_many :space_price_availabilities, through: :space_prices, :order => "day_of_week, begin_time", read_only: true
end
class SpacePrice < ActiveRecord::Base
belongs_to :space
has_many :space_price_availabilities, inverse_of: :space_price, :dependent => :destroy, :order => "day_of_week, begin_time"
accepts_nested_attributes_for :space_price_availabilities, :allow_destroy => true
end
class SpacePriceAvailability < ActiveRecord::Base
belongs_to :space_price
end
I know the workaround is to implement a Space.space_price_availabilities method. It is not sufficient to just always reference Space.space_prices.space_price_availabilities b/c I need the complete list flattened and sorted.
If I comment out the has_many :through in Space, I can create new Spaces fine. Then if I uncomment the has_many :through in Space, I can read them and update them fine. It’s only on create that I get the following exception.
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection
Checking the logs, the right SQL is being generated and called, but then hits this exception and rolls it back.
Any ideas? I’d really like to know if I’m missing something fundamental about the rails associations. It looks pretty straight-forward to me. The model is essentially the same as the example in the API documentation.
Add
:autosave => falseon