-
Notifications
You must be signed in to change notification settings - Fork 361
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
Comments
No. There is no way to create Active Record associations between models that are not Active Record models. |
@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) Many thanks, |
I created a PR that demonstrated a potential implementation for a |
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
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
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
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
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
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:
Many thanks.
Mauro
The text was updated successfully, but these errors were encountered: