Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## lista.rb
- class Lista < ActiveRecord::Base
- set_table_name "listas"
- validates_length_of :nome, :minimum=>4, :allow_nil => false, :message => ": Por favor, preencha o nome da lista maior que 4 caracteres."
- belongs_to :cliente, :class_name => "Cliente", :foreign_key => "id_cliente"
- def self.findBoth(conditions, order=:nome)
- @listasEstaticas = ListaEstatica.find(:all, :conditions=>conditions)
- @listasDinamicas = ListaDinamica.find(:all, :conditions=>conditions)
- # Getting the items
- @items = @listasEstaticas + @listasDinamicas
- # Resorting the items by name
- @items.sort_by {|lst| lst[order] }
- return @items
- end
- end
- ## lista_estatica.rb
- class ListaEstatica < Lista
- set_table_name "listas_estaticas" #:nodoc:
- belongs_to :cliente, :class_name => "Cliente", :foreign_key => "id_cliente"
- has_and_belongs_to_many :contatos, :class_name => "Contato", :join_table => "lista_estatica_contatos", :association_foreign_key => "id_contato", :foreign_key=>"id_lista_estatica", :uniq=>true #:nodoc:]
- def totalContatos
- contatos.length
- end
- end
- ## the SQL of creation
- CREATE TABLE listas
- (
- nome varchar(30),
- "type" varchar(20),
- id int8 NOT NULL,
- CONSTRAINT pk_lista PRIMARY KEY (id)
- )
- WITHOUT OIDS;
- CREATE TABLE listas_estaticas
- (
- id int8 NOT NULL DEFAULT nextval('sq_listas_estaticas'::regclass),
- id_cliente int8 NOT NULL,
- descricao text,
- id_lista int8,
- CONSTRAINT pk_listas_estaticas PRIMARY KEY (id),
- CONSTRAINT fk_listas_estaticas_cliente FOREIGN KEY (id_cliente)
- REFERENCES clientes (id) MATCH SIMPLE
- ON UPDATE NO ACTION ON DELETE CASCADE,
- CONSTRAINT fk_listas_estaticas_is_a_lista FOREIGN KEY (id_lista)
- REFERENCES listas (id) MATCH SIMPLE
- ON UPDATE NO ACTION ON DELETE CASCADE
- )
- WITH OIDS;
Add Comment
Please, Sign In to add comment