Administrate: HasOne support

Created on 29 Jan 2016  Â·  12Comments  Â·  Source: thoughtbot/administrate

has_one was merged to master in #55, but it has been removed. Is there any chance that we can add this back ? I tried added it on my own but it was very confusing. Any tips ?

Thanks

Most helpful comment

Hi all! Any update on this?

All 12 comments

@KatherineMuedas, that's odd. HasOne is still included in Administrate.

If you wanted to use it in a dashboard, it would look something like this:

class OrderDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    id: Field::Number,
    customer: Field::HasOne,
    # ...

  }end

If that doesn't work, can you post a code sample of how you're using it?

@graysonwright The link you posted above is 404ing, I think you meant to post

https://github.com/thoughtbot/administrate/blob/master/lib/administrate/field/has_one.rb

That said, I think @KatherineMuedas is right. You can't manipulate has_one relationships with administrate, you get a message that "has_one relationships are not supported."

I'm going to do a bit more research as to how this state came about.

Ah, I see what you mean. Administrate is able to display HasOne relationships, but doesn't let the user update them in forms.

Problem

I'm taking a look at how we'd support this, and one thing is currently blocking it. When the user submits a form with a has_one relationship, the updated association comes in as an id in the params. In this example, it's the order parameter:

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"abc123",
 "customer"=>{"name"=>"Scot Kirlin",
 "order"=>"2",
 "email"=>"[email protected]",
 "email_subscriber"=>"0"},
 "commit"=>"Update Customer",
 "id"=>"1"}

However, Rails won't let you update a has_one relationship with an id - you need to pass in an actual Rails model for the association to be assigned correctly.

Solution

One way to work around this would be to define a couple methods on your model, and use Administrate::Field::BelongsTo instead. As an example, that would look something like:

class Student < ActiveRecord::Base
  has_one :locker

  def locker_id=(value)
    self.locker = Locker.find(value)
  end

  def locker_id
    locker ? locker.id : nil
  end
end

Defining those two extra methods would make the has_one relationship behave exactly like a belongs_to, which would make it work with Administrate::Field::BelongsTo.

That suggestion is taken from http://stackoverflow.com/a/18740640.

This is not a solution for me since I need to edit the relationship.

Hi all! Any update on this?

Any update on this issue? Got the same issue here.

+1 for this issue!

@nickcharlton @graysonwright
Any idea why it was possible to do before but not anymore?

I'm working on a PR to enable HasOne but not sure what's the correct approach would be. In our application we need HasOne form nested because they are co-dependent.

Does inlining HasOne form sounds like something you would accept as a PR? Or you had something else in mind for HasOne?

+1 for this

+1 for the request

+1 for has_one relations

lol is this still not in?

Was this page helpful?
0 / 5 - 0 ratings