在循环中添加 Rails ActiveRecord 关联

发布于 2024-09-16 07:12:43 字数 521 浏览 2 评论 0原文

我想通过关联为数组中的每个符号添加一个 has_many 到 activerecord 模型类。例如,

PeopleOrganisation::ROLES.each do |role|
    has_many role.to_s.pluralize.to_sym, :through => :people_organisations, :source => :person,
      :conditions => "people_organisations.role = '#{role.to_s}'" do
      def << (object)
        PeopleOrganisation.send(:with_scope, :create => {:role => **role**}) { self.concat object }
      end
      end
  end

除了 def 方法中对角色变量的引用之外,一切正常。这是因为 def 方法不是闭包。有办法实现我想要的吗?

I want to add a has_many through association to a activerecord model class for each symbol in an array. for example

PeopleOrganisation::ROLES.each do |role|
    has_many role.to_s.pluralize.to_sym, :through => :people_organisations, :source => :person,
      :conditions => "people_organisations.role = '#{role.to_s}'" do
      def << (object)
        PeopleOrganisation.send(:with_scope, :create => {:role => **role**}) { self.concat object }
      end
      end
  end

everything works fine except for the reference to the role variable inside the method def. This is because the method def is not a closure. Is there a way of achieving what I want?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖阳 2024-09-23 07:12:43

试试这个:

PeopleOrganisation::ROLES.each do |role|
  has_many(role.to_s.pluralize.to_sym, 
             :through => :people_organisations, :source => :person,
             :conditions => ["people_organisations.role = ?", role]
  ) do
    define_method("<<") do |object|
      PeopleOrganisation.send(:with_scope, :create => {:role => role}) { 
        self.concat object 
      }
    end
  end
end

Try this:

PeopleOrganisation::ROLES.each do |role|
  has_many(role.to_s.pluralize.to_sym, 
             :through => :people_organisations, :source => :person,
             :conditions => ["people_organisations.role = ?", role]
  ) do
    define_method("<<") do |object|
      PeopleOrganisation.send(:with_scope, :create => {:role => role}) { 
        self.concat object 
      }
    end
  end
end
丑丑阿 2024-09-23 07:12:43

您可以尝试使用 define_method 方法,而不是使用 def 定义方法:

PeopleOrganisation::ROLES.each do |role|
  has_many role.to_s.pluralize.to_sym, :through => :people_organisations, :source => :person,
           :conditions => "people_organisations.role = '#{role.to_s}'" do
    define_method(:<<) do |object|
      PeopleOrganisation.send(:with_scope, :create => {:role => role}) { self.concat object }
    end
  end
end

Instead of defining method using def you can try define_method method:

PeopleOrganisation::ROLES.each do |role|
  has_many role.to_s.pluralize.to_sym, :through => :people_organisations, :source => :person,
           :conditions => "people_organisations.role = '#{role.to_s}'" do
    define_method(:<<) do |object|
      PeopleOrganisation.send(:with_scope, :create => {:role => role}) { self.concat object }
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文