Reputation: 735
I have 2 models (Paygrade and Empsal) for 2 controllers (paygrades and empsals) Here in the form of empsals in view (_form.html.erb)
# PayGrade <%= f.collection_select :pay_grade, Paygrade.all, :id, :title,
# :prompt => 'Select Paygrade' %>
PayGrade <%= f.collection_select :paygrade, Paygrade.all, :id, :title,
:prompt => 'Select Paygrade' %>
Model of Empsal
class Empsal
include Mongoid::Document
# field :pay_grade, type: String
belongs_to :paygrade
field :salary_component, type: String
field :pay_frequency, type: String
field :currency, type: String
field :amount, type: String
field :comments, type: String
end
Model of Paygrade
class Paygrade
include Mongoid::Document
has_many :empsals # +++++ added
validates_presence_of :title
field :title, type: String
end
I want to make the association such that it will save id of paygrade in Empsal for field pay_grade. But shows the related name of id in view.html.erb <% empsal.pay_grade %> what association should be done in model of (Paygrade and Empsal) to get the required output?
Upvotes: 0
Views: 421
Reputation: 16435
class Empsal
include Mongoid::Document
belongs_to :paygrade
end
Upvotes: 1