3

does anyone have a good resource for an effective way to store user address details so they can be accessed by multiple models? I found the below article, but its old and may be out of date for rails 4.

http://kconrails.com/2010/10/19/common-addresses-using-polymorphism-and-nested-attributes-in-rails/.

would this method still apply to rails 4 or would I have to add/remove certain pieces, like adding strong params?

heinztomato
  • 795
  • 1
  • 7
  • 12
  • the article is using a 'Polymorphic Associations' here: http://guides.rubyonrails.org/association_basics.html#polymorphic-associations. Also, this cast:http://railscasts.com/episodes/154-polymorphic-association-revised. And yes that applies to rails#4. I'm not sure what's your question here. – dima Feb 24 '15 at 14:11

1 Answers1

0

I think the method in the post is doing it properly, and it works in rails 4. The only thing that can be tweaked a little bit is in the migration. Instead of:

t.integer :addressable_id
t.string :addressable_type

Now you can do in one single line:

t.references :addressable, polymorphic: true

The addressable_id and addressable_type columns would be created automatically.

Hope this help.

Jiasen Xu
  • 301
  • 2
  • 6