Ruby on Rails -- has_and_belongs_to_many relationship
I am currently using a legacy database and I have 2 models: submission and publication.
class Submission < ActiveRecord::Base
has_and_belongs_to_many :publications, :join_table => "ish_sub_pub", :association_foreign_key => "SLN_PUB_FK"
end
class Publication < ActiveRecord::Base
has_and_belongs_to_many :submissions, :join_开发者_StackOverflowtable => "ish_sub_pub" ,
:association_foreign_key => "SLN_SUBMISSION_FK"
end
When i create a new submission, i want to be able to associate a publication to it and for this i will need to add a new record in the join table, 'ish_sub_pub'.
I am a bit confused about how to proceed with adding a publication to a submission.
Thanks a lot for your help
Assuming your publication is already instantiated in my_publication
, all you need to do is just_created_submission << my_publication
.
精彩评论