dimanche 13 mars 2016

ruby on rails - Assign has_and_belongs_to_many relashion

I have:

class Offer < ActiveRecord::Base
 ...
 has_and_belongs_to_many :tags
 ...
end

class Tag < ActiveRecord::Base
 ...
 has_and_belongs_to_many  :offers
 ...
end

and

def create
 ...
 @offer = @commercial_activity.offers.build(offer_params)
 tagsname = params[:tags].split(',')
 for tag in tagsname
   @offer.tags = Tag.find_or_create_by(name: tag, city_id: @commercial_activity.city)
  end
  ...
end

When I create a new offers I have this error:

undefined method `each' for #<Tag:0x9fcc918>

How can I solve this problem?

Thanks

Aucun commentaire:

Enregistrer un commentaire