0

I have implemented Friendly IDs and right now I am having trouble associating the opportunities for a company by slug (I had this working fine by ID).

CompaniesController:

  def show
    @opportunities = Opportunity.where("company_id = ?", params[:id]).paginate(:page => params[:page], :per_page => 25)
  end

The Opportunity model has a company_id column. (Company has_many Opportunities). So the above works perfectly is my URL is "companies/1".

Since I've implemented Friendly ID, I now have a "slug" column in my Company model. So essentially I need to match the Company ID to the Opportunity ID, then grab the slug?

I am trying something like this, but it is not working:

  def show
    @opportunities = Opportunity.where("company_id = ?", params[:id]).find(params[:slug]).paginate(:page => params[:page], :per_page => 25)
  end

How should I actually be doing this?

Thanks.

carols10cents
  • 6,943
  • 7
  • 39
  • 56

1 Answers1

0

You could use friendly finds, something like this

def show
  @company = Company.friendly.find(params[:id])
  @opportunities = @company.opportunities.paginate(:page => params[:page], :per_page => 25)
end

Hope this helps

strivedi183
  • 4,749
  • 2
  • 31
  • 38
  • I always try to make it more difficult than it is! Thanks very much. –  Nov 24 '13 at 22:48