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

Polymorphic Belongs To Issue #1228

Closed
rovermicrover opened this issue Jun 29, 2012 · 2 comments
Closed

Polymorphic Belongs To Issue #1228

rovermicrover opened this issue Jun 29, 2012 · 2 comments

Comments

@rovermicrover
Copy link
Contributor

I am unsure if this is a javascript bug or just my own ignorance.

My set up

source.rb

class Source < Person
  has_many :media, :as => :origin, :inverse_of => :origin
end

author.rb

class Author < Person
  has_many :media, :as => :creator, :inverse_of => :creator
end

medium.rb

class Medium < ActiveRecord::Base
  ...
  belongs_to :creator, :polymorphic => true, :inverse_of => :media
  belongs_to :origin, :polymorphic => true, :inverse_of => :media
  ...
end

blog.rb

class Blog < Medium
  ...
end

rails_admin.rb

config.model Blog do
    ...
    edit do
      ...
      field :creator
      field :origin
      ...
    end
  end

schema for medium

...
:creator_id => :integer,
:creator_type => :string,
:origin_id => :integer,
:origin_type => :string
...

When I go to edit a blog's origin or creator in rails admin, I get two drop downs for each, the first drop-down for the models and the second one for the records. The first issue is the drop-down on the right for records is empty, and if I select an actually model from the one of the left it just adds undefined as an option to the right side.

I then tried to edit to the other way around, ie set up on author, field :media. No media entries showed up.

If I force a creator/origin relationship through the console, everything in the console seems to be ok. I can do Blog.where(whatever).first.creator, and Author.where(whatever).first.media, and everything works just fine, and if I go into rails admin it will then show on the right hand selector after load #Author (Author.id), but it also loads into the list everyone person, not just possible creators.

But if I select anything from the left, the model, selector, Author for example, then it will still say undefined in the second option, and if I scroll back up to nothing select for model it will go back to being blank. The funny thing is that its querying for the right entries when I select a model. So when I select authors from the left selector its doing

Author Load (0.6ms)  SELECT `people`.* FROM `people` WHERE `people`.`type` IN ('Author') ORDER BY people.id desc

Do I just need to declare everything custom when it comes to the relationships in rails admin in this case, or am I just leaving out some simple peace of code, or have I run into a javascript bug sense its basically throwing undefined?

Using rails 3.2.6 and Head master of Rails_admin.

PS: Checked the console on Google Chrome, no JS errors.

PS2: Almost forgot also tried whitelisting based on the wiki, didn't help.
medium.rb

attr_accessible :creator_id, :creator_type, :origin_id, :origin_type

PS3: If I save it as author undefined, and then load it back to edit again, it has the author model selected and if I go to the drop-down for entries, its populated with my authors. If I though select a different model I get undefined again, and if I go back and select Author its also just gives me undefined. It has to be a javascript error, because once again its still querying the database for the correct entries associated with the select model, its just nothing get returned. I also noticed with normal has_many that aren't polymorphic that this issue happens when I remove

associated_collection_cache_all true

From their options. Has anyone else had this issue, or am I just running into some type of conflict?

PS4: Here is my gemfile.lock in case there is a know conflict I am over looking.
https://gist.github.com/3021412

@rovermicrover
Copy link
Contributor Author

Found a fix creating a pull request right now.

@rovermicrover
Copy link
Contributor Author

the pull request for the record
#1229

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

1 participant