You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 19, 2018. It is now read-only.
This code implements has_and_belongs_to_many in Hyperloop by using the existing has_many :through functionality and dynamically generating the join table.
ifRUBY_ENGINE == "opal"moduleActiveRecordmoduleClassMethodsdefhas_and_belongs_to_many(assoc,scope=nil, **options)other=assoc.singularizename=self.name.downcaserassoc=name.pluralizehabtm_name=[assoc,rassoc].sort.join("_")# alphabetical orderhabtm_class=habtm_name.singularize.camelizehas_manyhabtm_name# regular has_many makes the through workhas_manyassoc, **options.merge(through: habtm_name)returnif ::Object.const_defined?(habtm_class)# prevent duplication# double colon before Object and Class are needed - Opal bug?
::Object.const_set(habtm_class, ::Class.new(::ActiveRecord::Base)dobelongs_toother,foreign_key: "#{other}_id",inverse_of: assocbelongs_toname,foreign_key: "#{name}_id",inverse_of: rassocend)endendendend
Note that this patch alone only enables read only access. This is because Hyperloop doesn't support writing to has_many :through e.g. with the << method (but Rails does). You can't work around it by trying to create the join table directly because it doesn't exist on the server side so execute_remote fails.
The inverse_of isn't necessary but can't hurt I think. I hoped it would make the association writeable but it doesn't.