我正在尝试为Ruby on Rails has_and_belongs_to_many关联创建自己的setter方法。这是我到目前为止所得到的:
class Picture < ActiveRecord::Base
has_and_belongs_to_many :tags
def tags= comma_separated_tags
tag_objects = comma_separated_tags.split(',').collect do |tag_title|
Tag.find_or_create_by(title: tag_title)
end
@tags = tag_objects # doesn't work
# write_attribute(:tags, tag_objects) # raises an exception
end
end
有没有办法在不使用SQL的情况下在HABTM连接表上生成记录?