How to get the arel table of a habtm association?

拥有回忆 提交于 2019-12-05 11:34:40

Unfortunately, I believe your solution is pretty much the cleanest there is right now and is, in fact, what the association itself does internally when instantiated:

https://github.com/rails/rails/blob/46492949b8c09f99db78b9f7a02d039e7bc6a702/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb#L7

I believe the reflection.join_table they use vs reflection.options[:join_table] is only in master right now though.

Andrew

If you know the name of the association and therefore the join table, which in this case it looks like you do, you should be able to call:

Arel::Table.new(:groups_teachers)

In my testing that returns Arel table of a habtm association. Thanks to this answer for pointing this out to me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!