How can I save (insert) only associated objects without saving (updating) the base object?
For example I just want to save the phone numbers, I don’t wan to resave/update the person object.
def create_numbers
#params => person_id => 41, person => {:phone_number => '12343445, 1234566, 234886'}
@person = params[:person_id]
nums = params[:person][:phone_numbers].split(',')
nums.each do |num|
@person.phone_numbers.build(:number => num)
end
@person.save #here I just want to save the numbers, I don't want to save the person. It has read only attributes
end
Models:
Person < ...
# id, name
belongs_to :school, :class_name => :facility
has_many :phone_numbers
end
PhoneNumber < ...
# id, number
belongs_to :person
end
This is a bit of a dumb example, but it illustrates what I’m trying to accomplish
How about
@person.phone_numbers.create(:number => num)The downside is that you wont know whether it failed or not – you can handle that, but it depends on how exactly you want to handle it.