Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Associations between ActiveResource and ActiveRecord #292

Closed
mauro-ni opened this issue May 16, 2018 · 3 comments
Closed

Associations between ActiveResource and ActiveRecord #292

mauro-ni opened this issue May 16, 2018 · 3 comments

Comments

@mauro-ni
Copy link

Is there a way to create associations between ActiveResource models and ActiveRecord ones?
I tried to add a belongs_to association and I got the following error:

NoMethodError (undefined method `arel_table' for Project:Class)

Many thanks.
Mauro

@rafaelfranca
Copy link
Member

No. There is no way to create Active Record associations between models that are not Active Record models.

@mauro-ni
Copy link
Author

@rafaelfranca I can easily define methods on local resources in order to be able to load remote ones, but then I have N+1 query problems when working with collections.

Example: a collection of local resources (AR), each one belonging to a remote one (ARes)
How would you avoid this?

Many thanks,
Mauro

@basicBrogrammer
Copy link

I created a PR that demonstrated a potential implementation for a belongs_to_resource relationship. This could also be pulled out into a stand alone gem or left as a module that does not patch ActiveRecord upon initialization. #393

lfdebrux added a commit to alphagov/forms-admin that referenced this issue Feb 28, 2024
We would like groups to have zero or more forms.

Ideally we would do this without adding data to the form record in the
forms-api database about data in the forms-admin database. This leads us
to using a join table, so we can associate groups with forms but also do
the reverse lookup. Normally to use a join table in Rails we use the
`has_and_belongs_to_many` association [[1]]. However, models in
ActiveResource cannot be associated with models in ActiveRecord [[2]].
This means we can't use any of the usual methods to maintain the
association.

Instead, this commit takes the approach of having an explicit join
model. It's clunky, but is the best approach I've tried so far. It
doesn't rely too much on understanding Rails magic, and it doesn't
require writing any SQL.

Probably the proper fix for this issue is keeping the form records and
the group records in the same database, but that is outside the scope of
this ticket.

[1]: https://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association
[2]: rails/activeresource#292
lfdebrux added a commit to alphagov/forms-admin that referenced this issue Feb 28, 2024
We would like groups to have zero or more forms.

Ideally we would do this without adding data to the form record in the
forms-api database about data in the forms-admin database. This leads us
to using a join table, so we can associate groups with forms but also do
the reverse lookup. Normally to use a join table in Rails we use the
`has_and_belongs_to_many` association [[1]]. However, models in
ActiveResource cannot be associated with models in ActiveRecord [[2]].
This means we can't use any of the usual methods to maintain the
association.

Instead, this commit takes the approach of having an explicit join
model. It's clunky, but is the best approach I've tried so far. It
doesn't rely too much on understanding Rails magic, and it doesn't
require writing any SQL.

Probably the proper fix for this issue is keeping the form records and
the group records in the same database, but that is outside the scope of
this ticket.

[1]: https://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association
[2]: rails/activeresource#292
lfdebrux added a commit to alphagov/forms-admin that referenced this issue Feb 29, 2024
We would like groups to have zero or more forms.

Ideally we would do this without adding data to the form record in the
forms-api database about data in the forms-admin database. This leads us
to using a join table, so we can associate groups with forms but also do
the reverse lookup. Normally to use a join table in Rails we use the
`has_and_belongs_to_many` association [[1]]. However, models in
ActiveResource cannot be associated with models in ActiveRecord [[2]].
This means we can't use any of the usual methods to maintain the
association.

Instead, this commit takes the approach of having an explicit join
model. It's clunky, but is the best approach I've tried so far. It
doesn't rely too much on understanding Rails magic, and it doesn't
require writing any SQL.

Probably the proper fix for this issue is keeping the form records and
the group records in the same database, but that is outside the scope of
this ticket.

[1]: https://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association
[2]: rails/activeresource#292
lfdebrux added a commit to alphagov/forms-admin that referenced this issue Mar 4, 2024
We would like groups to have zero or more forms.

Ideally we would do this without adding data to the form record in the
forms-api database about data in the forms-admin database. This leads us
to using a join table, so we can associate groups with forms but also do
the reverse lookup. Normally to use a join table in Rails we use the
`has_and_belongs_to_many` association [[1]]. However, models in
ActiveResource cannot be associated with models in ActiveRecord [[2]].
This means we can't use any of the usual methods to maintain the
association.

Instead, this commit takes the approach of having an explicit join
model. It's clunky, but is the best approach I've tried so far. It
doesn't rely too much on understanding Rails magic, and it doesn't
require writing any SQL.

Probably the proper fix for this issue is keeping the form records and
the group records in the same database, but that is outside the scope of
this ticket.

[1]: https://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association
[2]: rails/activeresource#292
lfdebrux added a commit to alphagov/forms-admin that referenced this issue Mar 4, 2024
We would like groups to have zero or more forms.

Ideally we would do this without adding data to the form record in the
forms-api database about data in the forms-admin database. This leads us
to using a join table, so we can associate groups with forms but also do
the reverse lookup. Normally to use a join table in Rails we use the
`has_and_belongs_to_many` association [[1]]. However, models in
ActiveResource cannot be associated with models in ActiveRecord [[2]].
This means we can't use any of the usual methods to maintain the
association.

Instead, this commit takes the approach of having an explicit join
model. It's clunky, but is the best approach I've tried so far. It
doesn't rely too much on understanding Rails magic, and it doesn't
require writing any SQL.

Probably the proper fix for this issue is keeping the form records and
the group records in the same database, but that is outside the scope of
this ticket.

[1]: https://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association
[2]: rails/activeresource#292
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants