railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.88k stars 2.25k forks source link

Association display changed after migrating to rails_admin 3.0 #3521

Closed bricedurand closed 2 years ago

bricedurand commented 2 years ago

Hello,

I've migrated my application from rails_admin 2.2 to 3.0 and it is usable, however some views have changed without changing the model code.

I have a Student model which belongs to a School :

class Student < User
    belongs_to :school, optional: true

    rails_admin do
      list do
        field :school
      end

      edit do
        field :school
     end
  end
end

Here are the changes : List view In my 2.2 list view, the school was displayed as a link, which was a nice way to navigate in the admin. 2022-05-23 link to school in user list view rails_admin 2 Now in 3.0 list view, I lost the link : 2022-05-23 link to school in user list view rails_admin 3

Show view In 2.2 show view, I also had a link 2022-05-23 link to school in user show view rails_admin 2

And now in 3.0 show view it is just the ID : 2022-05-23 link to school in user show view rails_admin 3

Edit view In 2.2 edit view, there was a select box to change the school, plus a button to modify the school or add a new one. 2022-05-23 link to school in user edit view rails_admin 2

In 3.0 edit view there is only an input with the name of the school 2022-05-23 link to school in user edit view rails_admin 3

Is it possible to get the old display back? I haven't found anything regarding field configuration that would give me the old behaviour.

mshibuya commented 2 years ago

What's the exact version of RailsAdmin? Could you try these in Rails console and provide output?

RailsAdmin::Config.model(School).fields.find {|f| f.name == :school }
RailsAdmin::Config.model(School).show.fields.find {|f| f.name == :school }
bricedurand commented 2 years ago

Hello, thanks for taking the time to answer this.

The exact version of RailsAdmin is 3.0.0.

Here is the first output :

RailsAdmin::Config.model(School).fields.find {|f| f.name == :school }

Traceback (most recent call last):
/Users/brice/.rvm/gems/ruby-2.7.2@monstage/gems/rails_admin-3.0.0/lib/rails_admin/config/fields.rb:50:in `factory': undefined method `properties' for nil:NilClass (NoMethodError)

The second command has the same output :

 RailsAdmin::Config.model(School).show.fields.find {|f| f.name == :s
chool }

Traceback (most recent call last):
/Users/brice/.rvm/gems/ruby-2.7.2@monstage/gems/rails_admin-3.0.0/lib/rails_admin/config/fields.rb:50:in `factory': undefined method `properties' for nil:NilClass (NoMethodError)
mshibuya commented 2 years ago

Sorry it was my mistake, there ones:

RailsAdmin::Config.model(Student).fields.find {|f| f.name == :school }
RailsAdmin::Config.model(Student).show.fields.find {|f| f.name == :school }

I want to know how RailsAdmin detects that school field.

bricedurand commented 2 years ago

Ok, the Student model is in a module 'Users' so I ran it like this :

RailsAdmin::Config.model(Users::Student).fields.find {|f| f.name ==
 :school }

RailsAdmin::Config.model(Users::Student).show.fields.find {|f| f.na
me == :school }

Both return nil. There are other fields but not school.

I also ran this on model School

RailsAdmin::Config.model(School).fields

and had the same error as above

mshibuya commented 2 years ago

Thanks. To investigate the issue I need to be able to reproduce it. Could you try creating a new Rails application and generating relevant models to reproduce? If you can upload it to a public repo or gist it will be super-helpful.

bricedurand commented 2 years ago

I will try with a new app but I doubt that it is the best approach, because the issue appeared from migrating an existing a code base from RailsAdmin 2.2.1 to 3.0.0. So I'd rather remove code step by step and see when it is working again.

The code is already on a public repo : https://github.com/betagouv/monstage You can try running it but you might run into issues like broken db migrations. At least you have access to the whole source code.

I'll take some time to try to reproduce this issue and help you with the project setup if you want to investigate further.

mshibuya commented 2 years ago

Your app was too big to be fully run, but I partially tested it and found out that with the RailsAdmin master branch the school field was correctly detected.

> RailsAdmin.config(Users::Student).fields.find{|f| f.name == :school}
=> #<RailsAdmin::Config::Fields::Types::BelongsToAssociation[school] ...>

Could you also try that?

gem 'rails_admin', '~> 3.0', github: 'railsadminteam/rails_admin'
bricedurand commented 2 years ago

I tried using master branch and it worked !

I also realized this issue was only happening on my local machine, and not on production. Haven't found the reason why though...

mshibuya commented 2 years ago

My guess is it's something related to the initialization process. I won't go further for now, since #3541 seems to work. Thank you for reporting.