Пользователь указал поля динамической модели в направляющих

Я использую Python для protoyping, CGKit является невероятной библиотекой, с помощью PyOpenGL, легко создать играемую демонстрацию, и большинство хороших 3-х инструментов создания содержания поддерживает сценарии Python, которые важны получить Вас движение.

7
задан Ryan Bigg 17 October 2009 в 23:43
поделиться

2 ответа

I'm sorry I don't know of any plugin to do that. But I have an implementation suggestion.

The idea is to add a "DynamicField" model which would be a has_many relation to the Contact model. When you have a method missing in the Contact model, you check if there's a dynamic field to retrieve it if that's the case.

class DynamicField < ActiveRecord::Base
    belongs_to :contact
end


class Contact < ActiveRecord::Base
    has_many :dynamic_fields

    def method_missing(sym, *args, &block)
        begin
            super
        rescue
            field = dynamic_fields.find_by_name(sym)
        end
        raise ActiveRecord::NoMethodError if field.nil?
        field.value
    end
end

You will need to add a regex if you want to add virtual attributes with the attribute= method (detecting the presence of a "=" and doing an update instead of only getting the value). But you already have here the idea.

When the method doesn't exists, we check the dynamic fields if there is one with the same name. Если нет (field.nil?), Мы вызываем NoMethodError. В противном случае мы возвращаем его.

Чтобы вы могли получить список всех своих полей со следующим:

Contact.find(:first).dynamic_fields

И получить конкретное поле со следующим:

Contact.find(:first).my_dynamic_field
6
ответ дан 7 December 2019 в 05:25
поделиться

Вот еще один способ сделать это.

Заявление об ограничении ответственности: Этот метод не рекомендуется из-за возможности злоупотребления. Фактически, он должен быть доступен только администраторам вашего сайта.

class MyModel < ActiveRecord::Base
  ...
  def self.add_column(name, type, args= {})
    ActiveRecord::Migration.add_column table_name, name, type, args
  end
end
2
ответ дан 7 December 2019 в 05:25
поделиться
Другие вопросы по тегам:

Похожие вопросы: