belongs_to with :primary_key raises exception on rendering _form
Created by: iarie
Administrate 0.4 and Rails 5.02.
setup:
class User
belongs_to :group, foreign_key: :group_code, primary_key: :code
end
class Group
has_many :users, foreign_key: :group_code, primary_key: :code
end
and in UserDashboard we have
ATTRIBUTE_TYPES = {
...
group: Field::BelongsTo.with_options(primary_key: :code),
...
}
Showing {path to gem}/administrate-0.4.0/app/views/fields/belongs_to/_form.html.erb where line #23 raised:
undefined method `group_id' for #<User:0x007fa338a048c0>
Did you mean? group
group=
The cause of problem is hardcoded id at: https://github.com/thoughtbot/administrate/blob/master/lib/administrate/field/belongs_to.rb#L7
def self.permitted_attribute(attr)
:"#{attr}_id"
end
I didn't find a quick workaround, but i think it must be something like this :"#{attr}_#{primary_key}"
or I'm missing something.