title: Rails: How to create polymorphic relationship/friendship model
body: <p>I want to create a polymorphic friendship/network system.</p>
  
  <p>For example</p>
  
  <p>User can request or be requester to join multiple models like:</p>
  
  <ul>
  <li>Company</li>
  <li>Project</li>
  <li>Group</li>
  <li>Contacts(friends)</li>
  </ul>
  
  <p>Some will be one to many e.g. a user is employed by one company but a company has many employees.</p>
  
  <p>Others many to many e.g a user has many projects a project has many users</p>
  
  <p>The relationships aren't to difficult to create using active recorded. The middle step, requesting(invitations) that has me baffled.</p>
  
  <p>After bit of messing around this is what I came up with.</p>
  
  <p>The example below will allow me to make requests from a profile(user) to a multiple models polymophically(Is that even a word?), including the profile model itself.</p>
  
  <p>This is a sample of how the Controller could work.</p>
  
  <pre><code>@company = Company.find(params[:id])
  @request = @company.requests.create!(status: "pending").build_profile 
  @request.profile = current_user
  </code></pre>
  
  <p>How could I make @company more dynamic, say if it where a profile(user) requesting to join a project or a profile(user) requests to be friends with another profile(user)?</p>
  
  <p>So I have solved this little problem I think with the following code in my controller.</p>
  
  <p>requests_controller.rb</p>
  
  <pre><code>class RequestsController &lt; ApplicationController
    before_filter :load_requestable
  
    def new
      @request = @requestable.requests.new
    end
  
    def create
      @request = @requestable.requests.new(params[:status])
    if @request.save
      redirect_to @requestable, notice: "Request sent."
    else
      render :new
    end
  end
  
  private
  
  def load_resquestable
      resource, id = requests.path.split('/')[1, 2]
      @requestable = resource.singularize.classify.constantize.find(id)
  end 
  </code></pre>
  
  <p>[<strong>sticking point</strong>] Now I'm trying to get the request button in my view happening and am getting the following error</p>
  
  <pre><code>undefined method `requests_path' for #&lt;#&lt;Class:0x007fe8b26667f8&gt;:0x007fe8b26f40d0&gt; 
  </code></pre>
  
  <p>Extracted source (around line #1):</p>
  
  <pre><code>1: &lt;%= form_for [@requestsable, @request] do |f| %&gt;
  2:   &lt;div class="field"&gt;
  3:     &lt;%= f.text_area :content, rows: 8 %&gt;
  4:   &lt;/div&gt; 
  </code></pre>
  
  <p><strong>Here is the code for the views</strong></p>
  
  <p>companies/show.html.erb  </p>
  
  <pre><code>&lt;%= @company.name %&gt;
  &lt;% render 'requests/form' %&gt;
  </code></pre>
  
  <p>requests/_form.html.erb</p>
  
  <pre><code>&lt;%= form_for [@requestsable, @request] do |f| %&gt;
    &lt;div class="field"&gt;
      &lt;%= f.text_area :content, rows: 8 %&gt;
    &lt;/div&gt;
    &lt;div class="actions"&gt;
     &lt;%= f.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>_<em>models</em></p>
  
  <p>profile.rb</p>
  
  <pre><code>class Profile &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :company
    has_many :requests
    has_many :requested, as: :requestable
  
    attr_accessible :first_name, :last_name
  
  
    validates :first_name, presence: true
    validates :last_name, presence: true
  
  end
  </code></pre>
  
  <p>request.rb</p>
  
  <pre><code>class Request &lt; ActiveRecord::Base
  
     attr_accessible :status
     belongs_to :requestable , polymorphic: true
     belongs_to :profile
  
  end
  </code></pre>
  
  <p>company.rb</p>
  
  <pre><code>class Company &lt; ActiveRecord::Base
     has_many :employees, 
      :foreign_key =&gt; 'company_id', 
      :class_name =&gt; "Profile"
  
     has_many :requests, as: :requestable
  
  
    attr_accessible :name
    validates :name,  presence: true, length: { maximum: 50 }, uniqueness: true
  
  end
  </code></pre>