0

I have a model called users, in which users have usernames. I have another model called educators, which has many users. I want to put friendly_ids on the educators model, and I want the slug to include the username (which is not referenced in the educator model). Is there a way that I can define the slug candidates on the educator model to take the username from the user model? Many thanks.

Mel
  • 2,481
  • 26
  • 113
  • 273
  • What version of `friendly_id` are you using, and [what have you tried?](http://whathaveyoutried.com) I'll soon post an answer I think will help, but it would be easier to answer if I had some existing code to modify. :) – lime Mar 05 '14 at 13:20
  • Also, an example of a specific `Educator` and the slug you want to achieve would be great. – lime Mar 05 '14 at 13:28

1 Answers1

0

You could probably make the usernames available on the Educator model with a has_many :through statement.

class Educator < ActiveRecord::Base
  has_many :users
  has_many :usernames, through: :users
end

Now I'm thinking you should be able to use :usernames in your slug candidates definition. I'm not entirely sure of how friendly_id will react on a collection, but there you have the main idea.

Also, if you wanted only a specific username but the model indeed has_many :users then you would need some more specific logic for sorting out which username you want.

lime
  • 6,901
  • 4
  • 39
  • 50