Polymorphic assoc. with new attr_accessible changes

385 views
Skip to first unread message

Sean Rudford

unread,
Sep 6, 2011, 6:51:33 PM9/6/11
to rails_admin
Recently upgraded rails_admin and lost a lot of associations because
of the attr_accessible implementation with whitelisting fields. I
started adding in all my *_id fields to attr_accessible in the right
places but have been unable to get a polymorphic association to return
to how it was previously.

So a little background,

Address Model (polymorphic):

############################
class Address < ActiveRecord::Base

attr_accessible :title, :street1, :street2, :city, :state, :zipcode, :addressable_id, :addressable_type
# just added the last 2 trying to fix the problem
belongs_to :addressable, :polymorphic => true

...

end
############################

and, for example, a User Model:

############################
class User < ActiveRecord::Base

...

# Setup accessible (or protected) attributes for your model

attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me,
:gender, :phone, :email, :photo # what
goes here?

# common associations
has_many :addresses, :as => :addressable

...
end
############################

So any advice here would be super helpful. I'm hoping it's an easy fix
that I can't seem to figure out which attr needs to be allowed.

Thanks!

Benoit Bénézech

unread,
Sep 7, 2011, 4:23:57 AM9/7/11
to rails...@googlegroups.com
Just tried on dummy_app with Comment#commentable, no problem. Do you get a 'WARNING: Can't mass-assign protected attributes: XXXXXXable_(id|type)'?
Or is the Address#addressable field read-only? You just need to whitelist mass-assigned attributes, :addressable_type and :addressable_id, just like you did. No magic here.

Sean Rudford

unread,
Sep 7, 2011, 3:39:00 PM9/7/11
to rails_admin
Yeah I checked that out in the dummy app. The only difference I saw
was that I have a more complex initializer file, so I'd have something
like `field :addresses, :has_many_association`

Instead of having a list option like it used to, it just lists the
relations with links to the record. Here's a screenshot example of
what I'm talking about:

It was this http://cl.ly/2M1e0F120l1P3f053x1W

But now it's http://cl.ly/2f0e3b0V2E1B16073U03

Does that make sense?

Benoit Bénézech

unread,
Sep 8, 2011, 6:04:10 AM9/8/11
to rails...@googlegroups.com
The mass-assigned attribute is :addresses in User then. That's why it's read_only. 

The only thing is that it's not supported by default (hidden). Maybe it works, maybe it doesn't, not sure. That's why I thought you were talking about the other end of the relationship.

            # Hide owning ends of polymorphic associations in edit views as
            # they'd need special handling in RailsAdmin::AbstractObject that
            # has not been implemented
            if f.association? && f.association[:as]
              f.hide
            end

Sean Rudford

unread,
Sep 8, 2011, 6:39:32 PM9/8/11
to rails_admin
Hmm. Adding :addresses does not help.

I'll admit I'm a bit confused as to why you would want to hide them on
the owning end. In my situation it makes all kind of sense to manage
the addresses on the user edit form, pretty much replicating a user
address book.

Or am I looking at this the wrong way? Should this be a nested form
view that I need to create?

Thank you for your help so far. I appreciate it.

Benoit Bénézech

unread,
Sep 9, 2011, 10:01:47 AM9/9/11
to rails...@googlegroups.com

See here for an example (you need to attr_accessible the mass-assigned key, should be : collection_singular_ids (:address_ids). Check your params in your logs when you save a User
https://github.com/sferik/rails_admin/blob/master/spec/dummy_app/app/models/field_test.rb

Thanks for the report!

Kimer Seen

unread,
Sep 23, 2011, 5:56:45 AM9/23/11
to rails_admin
Hi,

It seems that a polymorphic association "has_one" fails with models
like address, business..
here is an example:

class User < ActiveRecord::Base
attr_accessible :name, :age, :address_id
has_one :address, :as => :addressable
end

class Address < ActiveRecord::Base

attr_accessible :street, :postal, :city, :addressable_id, :addressable_type
belongs_to :addressable, :polymorphic => true
end

OUTPUT nothing:
Address: -
optional

Although, it works with has_many association:

attr_accessible :name, :age, :address_ids
has_many :addresses, :as => :addressable

Any ideas?

BR,
Kimerseen

Kim Sergey

unread,
Oct 17, 2011, 7:49:33 AM10/17/11
to rails_admin
oops, sorry, works... some mistyping.
Reply all
Reply to author
Forward
0 new messages