ruby on rails - Using the same route resource to find objects in a polymorphic association -


i have rails 3 app user model has_one profile. i'm thinking of adding ability sign , create profile facebook , potentially other services. user still have 1 profile, created through app, facebook, etc. pretty common.

in thinking through implementation, largely settled on polymorphism. way keep third-party service-specific logic in profile object still have associated user. like:

class user < activerecord::base   belongs_to :profile, polymorphic: true end  class profile < activerecord::base   has_one :user, as: :profile, dependent: :destroy end  class facebookprofile < activerecord::base   has_one :user, as: :profile, dependent: :destroy end 

my problem, however, when comes routing. @ moment have profiles routing through profiles#show. inside action typical find like:

@profile = profile.find(params[:id]) 

if using sti, use find on 1 class because i'd subclassing other profile objects. polymorphism seems i'd have bit different. store profile_id on user. in show action do:

@user = user.where(profile_id: params[:id]) @profile = @user.profile 

that feels bit odd me, i'm wondering if there easier way polymorphic profile object directly id.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -