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

Unable to display has many relationship if "source" is used in the relationship #681

Closed
mooreds opened this issue Oct 28, 2016 · 2 comments
Labels
bug breakages in functionality that is implemented models models, associations and fetching the underlying data

Comments

@mooreds
Copy link

mooreds commented Oct 28, 2016

Hiya folks,

I have a model that has a has many through relationship, but the name of one of the sides is custom.

User model:

...
    has_many :user_brands
    has_many :followed_brands, through: :user_brands, inverse_of: :followers, source: :brand,
...

Brands model:

...
  has_many :user_brands
  has_many :followers, through: :user_brands, source: :user, inverse_of: :followed_brands
...

UserBrand model:

class UserBrand < ActiveRecord::Base
  acts_as_paranoid
  belongs_to :user
  belongs_to :brand
end

Everything works fine in the application (that is the relation works), but administrate complains when I add the following to BrandDashboard.ATTRIBUTE_TYPES

    followers: Field::HasMany.with_options(limit: 1000),

I see this error message:

uninitialized constant FollowerDashboard

I think this is because the dashboard has no way of knowing that followers are actually a user, and so the user dashboard should actually be used.

Is there a way to point that out?

I searched with google and through the issues of this project, but didn't see anything that seemed to help. In particular, it was't clear to me if it was possible to customize the hasmany code https://github.com/thoughtbot/administrate/blob/v0.2.0/lib/administrate/field/has_many.rb to display a different dashboard. Should I just subclass the hasmany class?

I also worked around it by modifying the view to special case the handling of the 'followers' attribute, but that seems like a hack.

I'm using administrate 0.2.0 with rails 4.2.

Thanks.

@mooreds mooreds changed the title Unable to display has many relationship if source is used in the relationship Unable to display has many relationship if "source" is used in the relationship Oct 28, 2016
@courtsimas
Copy link

courtsimas commented Aug 17, 2019

I believe you can use the with_options on the HasMany declaration to specify the class name and foreign key and it should work.

@nickcharlton nickcharlton added bug breakages in functionality that is implemented models models, associations and fetching the underlying data and removed follow up labels Jan 2, 2020
@nickcharlton
Copy link
Member

I'm going to close this as it's been quiet for a long time and there's a potential solution, bug please open a new issue if this is still a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug breakages in functionality that is implemented models models, associations and fetching the underlying data
Projects
None yet
Development

No branches or pull requests

4 participants