I have has_many_through relationship:
class Schedule < ActiveRecord::Base
has_many :schedule_tasks
has_many :tasks, :through => :schedule_tasks
class Task < ActiveRecord::Base
has_many :schedule_tasks
has_many :schedules, :through => :schedule_tasks
When I create the record, the join table (schedule_tasks) record that's created only populates the bare minimum fields to make the join, in this case, :schedule_id and :task_id, I need an additional field - :day_id filled as well (this field is also in the Task model, but right now this is how I need to do it:
def create
@schedule = Schedule.find(task_params['schedule_id'])
@task = @schedule.tasks.create(task_params)
@schedule_task = @schedule.schedule_tasks.where(task_id: @task.id, schedule_id: @schedule.id).first
@schedule_task.day_id = @task.day_id
@schedule_task.save
redirect_to @schedule
end
Is there a simpler way to do this?