Reputation: 45
Im a little rusty to Rails and I'm trying to find an appropriate way to set a series of has_and_belongs_to_many associations. I have created a method, which I intend to make private, which sets the associations.
def update
def set_associations
industriesParams = params[:business][:industry_ids].reject{ |c| c.empty? }
@business.update(:industry_ids => industriesParams)
end
@business = Business.find(params[:id])
@business.set_associations
end
In this attempt, I am getting "undefined method `set_associations'" which I don't quite get. But I am also seeking a cleaner way of setting the associations from scratch.
Thanks, in advance.
Upvotes: 0
Views: 30
Reputation: 5172
This should do the work :
def update
@business = Business.find(params[:id])
set_associations
end
private
def set_associations
industriesParams = params[:business][:industry_ids].reject{ |c| c.empty? }
@business.update(:industry_ids => industriesParams)
end
But to make things shorter, you could even write the following :
def update
@business = Business.find(params[:id])
industriesParams = params[:business][:industry_ids].reject{ |c| c.empty? }
@business.update(:industry_ids => industriesParams)
end
Upvotes: 1