0

I created a refinanciamento and my show doesn't correct.

show.html.erb

<p>
  <strong>Nome:</strong>
  <%= @funcionario.pessoa.nome %>
</p>

<p>
  <strong>Matrícula:</strong>
  <%= @funcionario.matricula %>
</p>

When I put @funcionario.first.pessoa.nome, it "work" but, ever return the first, I don't wanna this, sure.

My controller:

  def show
    @funcionario = Funcionario.pesquisa_cpf(params[:pesquisa_func_cpf]).first
    @refinanciamento = Refinanciamento.find(params[:id])
  end

Example, when I register a refinanciamento, my url on show is: /refinancimento/100, this refinanciamento have the values: id: 100, funcionario_id: 2 I need a where that show the funcionario that have this refinanciamento. How I build this consult?

My models are:

class Refinanciamento < ActiveRecord::Base
  belongs_to :funcionario
(...)

class Funcionario < ActiveRecord::Base
  has_many :refinanciamentos
(...)

I just want show nome and matricula of funcionario that have the refinanciamento created. Thanks!

Elton Santos
  • 571
  • 6
  • 32
  • "I just want show nome and matricula of funcionario that have the refinanciamento created." You may want to provide some translations to give the reader context. – B Seven Apr 08 '16 at 18:25
  • Sorry, but my system is in portuguese, because this I don't translate, ok? Is "nome" and "matricula" – Elton Santos Apr 08 '16 at 18:28
  • I'm guessing nome is "name", what do you mean by "matricula", "pesquisa_cpf" and "Consult"? – Anthony E Apr 08 '16 at 18:32
  • matricula can be "register"; pesquisa_cpf can be "search_cpf" and consult is consult. – Elton Santos Apr 08 '16 at 18:36
  • 1
    I believe `consult` is `query`. `pesquisa_cpf` is a method that searches people from their document number. – MurifoX Apr 08 '16 at 18:37
  • I meant: How I build this query?. Pesquisa_cpf is a method, but I guess it has nothing, can remove quietly – Elton Santos Apr 08 '16 at 18:40

1 Answers1

2

First of all, Rails is an opinionated framework and assume some patterns to facilitate the developer life, like implementing everything in english, from classes names to attributes. So you are losing too much programming in your native language (You can always I18n everything to translate the texts and stuff, the code should be in english).

Second, and right to the problem, you have an association between refinanciamento and funcionario. If a refinanciamento belongs_to a funcionario, when you have a refinanciamento object, you can use @refinanciamento.funcionario and get all of its data like nome and matricula.

def show
  @refinanciamento = Refinanciamento.find(params[:id])
  # Here you can access funcionario's properties like
  # @refinanciamento.funcionario.pessoa.nome
  # @refinanciamento.funcionario.matricula
  # etc...
end
MurifoX
  • 14,991
  • 3
  • 36
  • 60