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!
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