I am trying to use the has_many :through relation in rails to return a set of product features. Please see this gist for the models: https://gist.github.com/4572661
I know how to do this using the ProductFeature model directly, but I really do not want to have to interact with it directly.
I want to be able to do this:
features = Product.features
So it returns:
[id: 1, name: 'Colour', value: 'Blue'],
[id: 2, name: 'Size', value: 'M'],
[id: 3, name: 'Shape', value: 'Round']
But I can only get it to return:
[id: 1, name: 'Colour'],
[id: 2, name: 'Size'],
[id: 3, name: 'Shape']
I was using this as a starting point.
has_many :throughis designed to regard thejoin tableas nothing more than that.Any columns on the join will be ignored from the association.
As such we have to use
product_featuresThereby we can say
If you use this type of thing a lot, I’d be inclined to do something like this;