I have an application that includes two tables - Companies and Company_Listings. A Company may have one or more Company_Listings. Currently the application sets the company.id in cache (e.g. Rails.cache.write("company", @company) ) and within the Company_Listings controller I access the Company.id via @company = Rails.cache.read("company").
I now realise I should not be doing this and should be accessing the Company.Id from the routes (at least that is my understanding). This I can't do.
The relevant code including the changes routes follows.
Company Model
class Company < ActiveRecord::Base
...
has_many :company_listings, dependent: :destroy
CompanyListing Model
class CompanyListing < ActiveRecord::Base
...
belongs_to :company
Routes
scope '/company' do
resources :company_details, only: [:new_listed_option,:index, :new, :create]
match 'company_listings/listed_option_listing',
:to => 'company_listings#new_listed_option', :as => 'listed_option_listing',
:via => :get
match 'company_listings/unlisted_option_listing',
:to => 'company_listings#new_unlisted_option', :as => 'unlisted_option_listing',
:via => :get
resources :company_listings, only: [:new_listed_option, :new, :create, :edit,:destroy] do
collection do
get 'edit_multiple'
get 'update_multiple' # put in to fix Heroku at least I thought that
post 'update_multiple'
end
end
end
When editing a Company the route displays as http://quiet-fortress-3338.herokuapp.com/company/company_listings/211/edit In this case 211 refers to he Company (as opposed to one of the comapny_listing.id).
Within the Company Listings controller how do I access the Comany.id (in this case 211 ? I have tried variations of
@company = Company.find(params[:id])
Thanks in advance Pierre
The Companies model
params.require(:company).
permit(:full_name)
The Company_Listings model
params.require(:company_listing).
permit(:company_id,
:share_price_dollars,
:option_unlisted,
:exchange_id,
:option_name,
:option_expiration_date,
:option_strike_price_dollars,
:share_number,
:company_code )
Therefore when adding a new option, which is one possible Company_Listing I want the Company.Id so the option has this link to the Companies table.