Here is my problem:
class Facility < ActiveRecord::Base
...
has_and_belongs_to_many :languages, :autosave => false, :join_table => 'facilities_languages'
...
end
When I do something like this:
facility = Facility.find(1)
language = Language.find(1)
facility.languages << language
Rails always do the SQL request:
"INSERT INTO `facilities_languages` (`language_id`,`facility_id`) VALUES (1, 1)"
Is there a way to avoid database requests unless I call ‘facility.save’ ?
Apparently, :autosave option here does something else.
Your Problem is that you use
has_and_belongs_to_manywhich isnt based on aModelso you cant access it as an Object. You could create aModelthat is doing the associations and then you could create new objects of this association model. e.g.:In the Model
In the other models:
Faculty
Language
Then you would be able to add associations by creating association objects which arent saved automaticly. For example you have a
Stringcontaining ids that contains the ids of the language objects which should be associated calledlanguage_idsin theFacultyController:Of cause this code should be conditioned and positioned after the save of faculty.