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

belongs_to with :primary_key raises exception on rendering _form #777

Closed
iarie opened this issue Mar 13, 2017 · 3 comments
Closed

belongs_to with :primary_key raises exception on rendering _form #777

iarie opened this issue Mar 13, 2017 · 3 comments

Comments

@iarie
Copy link
Contributor

iarie commented Mar 13, 2017

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.

@nickcharlton
Copy link
Member

Ah, you're right! Looks like it should be pulling in the primary_key too. Do you think you'd be able to open a PR for this?

@iarie
Copy link
Contributor Author

iarie commented Mar 16, 2017

@nickcharlton i'll investigate it further anytime soon

jcmorrow added a commit that referenced this issue Mar 24, 2017
Resolves #777.

Changes:

- Allow permitted_attribute to receive options, which can include
  primary_key. Use primary_key if specified, otherwise use `id`.
- Change `orders` primary key to `code` and `payments` foreign key to
  `order_code`.
- Remove `created_at` and `updated_at` from `Payment`'s
  `ATTRIBUTE_TYPES` because the `payments` table doesn't actually have
  those columns.
- Add `edit` route for `payments`.
jcmorrow added a commit that referenced this issue Mar 24, 2017
Resolves #777.

Changes:

- Allow permitted_attribute to receive options, which can include
  primary_key. Use primary_key if specified, otherwise use `id`.
- Change `orders` primary key to `code` and `payments` foreign key to
  `order_code`.
- Remove `created_at` and `updated_at` from `Payment`'s
  `ATTRIBUTE_TYPES` because the `payments` table doesn't actually have
  those columns.
- Add `edit` route for `payments`.
jcmorrow added a commit that referenced this issue Mar 30, 2017
Resolves #777.

Changes:

- Allow permitted_attribute to receive options, which can include
  primary_key. Use primary_key if specified, otherwise use `id`.
- Change `orders` primary key to `code` and `payments` foreign key to
  `order_code`.
- Remove `created_at` and `updated_at` from `Payment`'s
  `ATTRIBUTE_TYPES` because the `payments` table doesn't actually have
  those columns.
- Add `edit` route for `payments`.
@iarie iarie changed the title belongs_to with :primary_key rises exception on rendering _form belongs_to with :primary_key raises exception on rendering _form Mar 31, 2017
@iarie
Copy link
Contributor Author

iarie commented Apr 1, 2017

@nickcharlton Hello, Nick, if you'll have some time could you check my PR above please? It's pretty simple. I decided to implement foreign_key option instead of forcing to pull primary_key . I added few specs, and demonstration of this case in example_app.

jcmorrow added a commit that referenced this issue Apr 7, 2017
Resolves #777.

Changes:

- Allow permitted_attribute to receive options, which can include
  primary_key. Use primary_key if specified, otherwise use `id`.
- Change `orders` primary key to `code` and `payments` foreign key to
  `order_code`.
- Remove `created_at` and `updated_at` from `Payment`'s
  `ATTRIBUTE_TYPES` because the `payments` table doesn't actually have
  those columns.
- Add `edit` route for `payments`.
@iarie iarie closed this as completed Jul 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants