通过插件在模型中添加has_many关联--Rails

时间:2022-09-29 05:28:16

I have a rails project where project model has many issues.

我有一个rails项目,项目模型有很多问题。

class Project < ActiveRecord::Base
    has_many :issues
end

Now through a plugin i want to add an association in the project model, say has_many :activities

现在通过一个插件,我想在项目模型中添加一个关联,比如说has_many:activities

so that project model will look like

所以项目模型看起来像

class Project < ActiveRecord::Base
    has_many :issues
    has_many :activities
end

where activity is a model of my plugin.

其中activity是我的插件的模型。

Can this be done using hooks? Please give some example.

这可以用钩子完成吗?请举个例子。

2 个解决方案

#1


1  

Give this a whirl:

给这个旋转:

module ProjectExt
  def self.included base
    base.has_many :activities
  end
end

require 'project'    
Project.send(:include, ProjectExt)

#2


2  

You can add it by

你可以添加它

Project.has_many :activities

You can do that in your plugin after getting the class where you association is.

在获得关联所在的类之后,您可以在插件中执行此操作。

#1


1  

Give this a whirl:

给这个旋转:

module ProjectExt
  def self.included base
    base.has_many :activities
  end
end

require 'project'    
Project.send(:include, ProjectExt)

#2


2  

You can add it by

你可以添加它

Project.has_many :activities

You can do that in your plugin after getting the class where you association is.

在获得关联所在的类之后,您可以在插件中执行此操作。