Guest User

Untitled

a guest
Feb 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. ## lista.rb
  2. class Lista < ActiveRecord::Base
  3. set_table_name "listas"
  4.  
  5. validates_length_of :nome, :minimum=>4, :allow_nil => false, :message => ": Por favor, preencha o nome da lista maior que 4 caracteres."
  6.  
  7. belongs_to :cliente, :class_name => "Cliente", :foreign_key => "id_cliente"
  8.  
  9. def self.findBoth(conditions, order=:nome)
  10. @listasEstaticas = ListaEstatica.find(:all, :conditions=>conditions)
  11. @listasDinamicas = ListaDinamica.find(:all, :conditions=>conditions)
  12.  
  13. # Getting the items
  14. @items = @listasEstaticas + @listasDinamicas
  15.  
  16. # Resorting the items by name
  17. @items.sort_by {|lst| lst[order] }
  18.  
  19. return @items
  20. end
  21.  
  22. end
  23.  
  24. ## lista_estatica.rb
  25. class ListaEstatica < Lista
  26. set_table_name "listas_estaticas" #:nodoc:
  27.  
  28. belongs_to :cliente, :class_name => "Cliente", :foreign_key => "id_cliente"
  29.  
  30. 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:]
  31.  
  32. def totalContatos
  33. contatos.length
  34. end
  35.  
  36. end
  37.  
  38. ## the SQL of creation
  39. CREATE TABLE listas
  40. (
  41. nome varchar(30),
  42. "type" varchar(20),
  43. id int8 NOT NULL,
  44. CONSTRAINT pk_lista PRIMARY KEY (id)
  45. )
  46. WITHOUT OIDS;
  47. CREATE TABLE listas_estaticas
  48. (
  49. id int8 NOT NULL DEFAULT nextval('sq_listas_estaticas'::regclass),
  50. id_cliente int8 NOT NULL,
  51. descricao text,
  52. id_lista int8,
  53. CONSTRAINT pk_listas_estaticas PRIMARY KEY (id),
  54. CONSTRAINT fk_listas_estaticas_cliente FOREIGN KEY (id_cliente)
  55. REFERENCES clientes (id) MATCH SIMPLE
  56. ON UPDATE NO ACTION ON DELETE CASCADE,
  57. CONSTRAINT fk_listas_estaticas_is_a_lista FOREIGN KEY (id_lista)
  58. REFERENCES listas (id) MATCH SIMPLE
  59. ON UPDATE NO ACTION ON DELETE CASCADE
  60. )
  61. WITH OIDS;
Add Comment
Please, Sign In to add comment