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
refinanciamentoandfuncionario. If arefinanciamentobelongs_toafuncionario, when you have arefinanciamentoobject, you can use@refinanciamento.funcionarioand get all of its data likenomeandmatricula.