1

insert_into_file我一直在尝试使用自定义生成器中的方法将一些新关联插入到现有模型中。出于美学原因,并避免硬编码任何东西,我想在找到的最后一个之后插入它们。我尝试使用在 Rubular 中工作的正则表达式来实现这一点,但在我运行生成器时却没有:

def add_associations_to_other_things
  content = <<-CONTENT
has_many :things
  CONTENT
  inject_into_file "app/models/other_things.rb", \
  content, \
  :after => /(?:(has_and_belongs_to_many|has_many|has_one|belongs_to)[^\n]*$)(?!.*has_and_belongs_to_many|has_many|has_one|belongs_to.*\z)/m
end

换句话说,这应该找到最后一个关联到一个没有更多关联的换行符。该m选项意味着负前瞻中的点应被视为换行符,因此应注意文件末尾。在 Rubular 中,选择了最终的关联,但是当运行生成器时,内容会插入到每个关联之后。

我的正则表达式有什么问题?

4

0 回答 0