English 中文(简体)
我如何创造像超级阶级中的财产和在子类中的人口结构等tr块?
原标题:How do I create attr setter like property in superclass, and populate in subclasses?

在铁路中,我有一个家长班,继承自现役。 I m 试图在该类别中进行自由文字搜索,加上其他询问,这样,所有从中继承的阶层都能够使用自己的田地,根据模型进行改变:

#
# in base class
#
class GenericBase < ActiveRecord::Base

  named_scope :freetext, lambda { |query|
    if query.present?
      { :conditions => [ self.freetext_fields.join(  LIKE ? or  ), 
                         ( ["%#{query}%"]*self.freetext_fields.size ) ].flatten }
    else
      {}
    end
  }

end

#
# in inheriting class
#
class Person < GenericBase
  set_freetext_fields %w(firstname lastname username email)    
end

# or 
class Address < GenericBase
  set_freetext_fields %w(street city)    
end

#
# in controller
#
def search
  @people = Person.freetext(params[:query])
end

在上述例子中,我如何执行<代码>_freetext_fields。 可在<代码>GenericBase的所有继承模式中轻易使用。 这应当与set_table_name非常相似。

我想在母体或组合式模块中加以实施,这样,继任班的APIC将尽可能清洁。

最佳回答

你们可以执行这样的内容:

module A
    def set_freetext_fields=(*args)
     @a = args
    end

    def some_meth
     @a 
    end
end

class C
 extend A   

 C.set_freetext_fields = %w(firstname lastname username email)
end

puts C.set_freetext_fields

GenericBase

问题回答

暂无回答




相关问题
Subclass check, is operator or enum check

A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a ...

C++ Class Inheritance problem

Hi I have two classes, one called Instruction, one called LDI which inherits from instruction class. class Instruction{ protected: string name; int value; public: Instruction(string ...

Overloading a method in a subclass in C++

Suppose I have some code like this: class Base { public: virtual int Foo(int) = 0; }; class Derived : public Base { public: int Foo(int); virtual double Foo(double) = 0; }; ...

Embedding instead of inheritance in Go

What is your opinion of this design decision? What advantages does it have and what disadvantages? Links: Embedding description

Extending Flex FileReference class to contain another property

I want to extend the FileReference class of Flex to contain a custom property. I want to do this because AS3 doesn t let me pass arguments to functions through event listeners, which makes me feel sad,...

Interface Inheritance in C++

I have the following class structure: class InterfaceA { virtual void methodA =0; } class ClassA : public InterfaceA { void methodA(); } class InterfaceB : public InterfaceA { virtual ...

热门标签