Diary of problems and solutions related to web programming, database development and related technologies and platforms.

Associating multiple foreign keys to a migration table or a primary key

Step: Generating Scaffold
We are referencing the table codeorganization for creating multiple foreign keys for a same primary key ie codeorganisation_id
rails g scaffold dataform report_date:date organisation:references organisation_reported:references
Step: Point the referenced id to the model and make changes in Model
Change
class Dataform < ActiveRecord::Base
  belongs_to :organisation
  belongs_to :organisation_reported
end
To
class Dataform < ActiveRecord::Base
  belongs_to :organisation, :class_name => "Codeorganisation"
  belongs_to :organisation_reported, :class_name => "Codeorganisation"
end
Step: By default the those fields in the Controllers do not change and the fields are not in permitted mode when we try to add or edit the dataform records Change
def dataform_params
  #params.require(:dataform).permit(:codeorganisation_id, :codeorganisation_id)
end
To
def dataform_params
  #params.require(:dataform).permit(:organisation_id, :organisation_reported_id)
end 
For this, also refer the following related articles

No comments :

Post a Comment