ruby on rails 3 - FriendlyId suggest -
i want use friendlyid achieve localhost3000/users/edu/profile
not know how it! have these models user , userprofile
class user < activerecord::base has_one :user_profile, :dependent => :destroy extend friendlyid friendly_id :name, :use => :slugged end class userprofile < activerecord::base attr_accessible :user_id, :name, :surname, :nickname belongs_to :user end
how load in name
of user name
of userprofile? , how update name of user when name of userprofile changes?
for first used
class user < activerecord::base ... def name if user_profile "#{user_profile.name}" end end
but can't make change when update or create new profile in userprofile.
using ruby 1.9.3 , rails 3.2.13.
if understood correctly, problem sharing data between models, not friendlyid.
it seems delegate
best bet here. it's method in activesupport allows 1 model expose model's methods own.
class user < activerecord::base delegate :name, :name=, :to => :user_profile end
reference: http://api.rubyonrails.org/classes/module.html#method-i-delegate
the reason delegate both :name
, :name=
former method allows read attribute (getter), while latter allows write (setter).
before making these changes you'll want run migration remove name
field users
table in database, since on you'll using data in other model.
rails g migration remove_name_from_users name:string
Comments
Post a Comment