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

has_many associations with custom foreign_key and primary_key break edit views. #1878

Closed
submitteddenied opened this issue Dec 31, 2013 · 4 comments
Milestone

Comments

@submitteddenied
Copy link

Given two models, User and Invitation:

class User < ActiveRecord::Base
  #attribute :email
  has_many :invitations, primary_key: :email, foreign_key: :invitee_email
end

class Invitation < ActiveRecord::Base
  #attribute :invitee_email
  belongs_to :invitee, class_name: "User", primary_key: :email, foreign_key: :invitee_email
end

Rails admin throws a NoMethodError error when loading the edit page NoMethodError in RailsAdmin::Main#edit

ActionView::Template::Error (undefined method `email' for #<Invitation:0x007fb0e2adfc88>):
    17:   collection = if xhr
    18:     selected.map { |o| [o.send(field.associated_object_label_method), o.send(field.associated_primary_key)] }
    19:   else
    20:     i = 0
    21:     controller.list_entries(config, :index, field.associated_collection_scope, false).map { |o| [o.send(field.associated_object_label_method), o.send(field.associated_p
rimary_key)] }.sort_by {|a| [selected_ids.index(a[1]) || selected_ids.size, i+=1] }
    22:   end
    23: 
  activemodel (4.0.2) lib/active_model/attribute_methods.rb:439:in `method_missing'
  activerecord (4.0.2) lib/active_record/attribute_methods.rb:155:in `method_missing'
  rails_admin (0.6.0) app/views/rails_admin/main/_form_filtering_multiselect.html.haml:20

I tracked this down to be on line 21 where field.associated_primary_key is sent to o, this needs to be field.foreign_key.

I'll submit a pull request soon (if I make this change it fixes my issue), but I'm not sure what else my change might break.

@Alexander-Senko
Copy link
Contributor

I also have a quick fix for myself, but there is one interesting thing… The Rails have a very similar bug: rails/rails#14439. Maybe we shall wait for them to make a consistent fix then?

But we can wait for ages considering their bureaucracy… 8(

@daveyoon
Copy link

daveyoon commented Nov 6, 2015

is this still an open issue? Just ran into it.

@asiniy
Copy link

asiniy commented Nov 13, 2015

+1. Same issue

@dannatobigodo
Copy link

dannatobigodo commented May 14, 2018

+1. I temporarily configured the association invisible for the edit action.

class User < ActiveRecord::Base
  has_many :phone_numbers, foreign_key: :user_custom_key, primary_key: :custom_key

  rails_admin do
    edit do
      configure :phone_numbers do
        visible false
      end
    end
  end
end

class PhoneNumber < ActiveRecord::Base
  belongs_to :user, foreign_key: :user_custom_key, primary_key: :custom_key
end

Apparently the problem does not afflict the belongs_to side.

@mshibuya mshibuya added this to the 2.0.0 milestone Jun 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants