I find it difficult to understand how to create new records in a table and automatically create related relationships in a joined table.
Here are my models:
class Building < ActiveRecord::Base
has_many :user_buildings
has_many :users, :through => :user_buildings
end
class User < ActiveRecord::Base
has_many :user_buildings
has_many :buildings, :through => :user_buildings
....
end
class UserBuilding < ActiveRecord::Base
belongs_to :user
belongs_to :building
end
Now my user model is also used for development, so I use the current_user helper.
To get all the buildings that I use
current_user.buildings
So, from there I thought I could use
current_user.buildings.build
create a new building associated with the user and update the joined table; however, this adds the building to the building table and does not make an association in the user_buildings table.
I read the documentation at http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html , but I cannot figure out which direction I need to go.
Thank!