-
-
Notifications
You must be signed in to change notification settings - Fork 63
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
Selecting associated models #156
Comments
This would be a huge timesaver for us, I'm having to edit all forms of models that have associations in order to get this feature. Perhaps the association scope could be an option of the |
I have been able to workaround this with a custom field. It is a very quick'n dirty patch but it works for me. Here is the code for the class BelongsTo < Madmin::Fields::BelongsTo
def options_for_select(record)
all_options_for class_for_association record
end
private
def class_for_association(record)
record.class
.reflect_on_all_associations(:belongs_to)
.find { |el| el.name == attribute_name }
.klass
end
def all_options_for(klass)
resource = Madmin.resource_by_name klass.name
sorting = sorting_for resource
klass.all.order(sorting).map do |record|
[ resource.display_name(record), record.id ]
end
end
def sorting_for(resource)
column = resource.try :default_sort_column
direction = resource.try :default_sort_direction || "desc"
"#{column} #{direction}"
end
end Then in your resource specify to use the field: class FooResource < Madmin::Resource
attribute :bar, field: BelongsTo
# ...
end There are a few issue with my code, the most important is that it loads all the records for the association. |
I was wondering if it was possible to enable using associated models during creation. For example, I am trying to create a model which has some associations. It's an Activity model which belongs_to an ActivityCategory model. During creation on the /madmin panel I can't select any category. Same goes other way around. Is there a way to select these?
The text was updated successfully, but these errors were encountered: