Skip to content

Instantly share code, notes, and snippets.

@cmilfont
Created September 9, 2009 19:01
Show Gist options
  • Save cmilfont/183980 to your computer and use it in GitHub Desktop.
Save cmilfont/183980 to your computer and use it in GitHub Desktop.
#Models
class Vaga < ActiveRecord::Base
has_many :idioma_vagas
has_many :idiomas, :through => :idioma_vagas
accepts_nested_attributes_for :idiomas, :allow_destroy => true
accepts_nested_attributes_for :idioma_vagas, :allow_destroy => true
def idiomas_attributes=(attributes)
attributes.each{|i|
idioma = Idioma.find(:id => i[:id].to_i)
self.idiomas << idioma
}
end
end
class Idioma < ActiveRecord::Base
has_many :idioma_vagas
has_many :vagas, :through => :idioma_vagas
end
class IdiomaVaga < ActiveRecord::Base
belongs_to :vaga
belongs_to :idioma
end
#views
<% form_for(@vaga) do |f| %>
<%= f.error_messages %>
<%= f.label :nome %>
<%= f.text_field :nome %>
<% prefix = "vaga[idiomas_attributes][]" %>
<% fields_for prefix, :idiomas, :index => nil do |i| %>
<p>
<div>
<%= i.label :id, 'Idioma:' %>
<%= i.select :id, Idioma.all.collect {|k| [k.nome, k.id]}, :include_blank => true %>
</div>
</p>
<% end %>
<p>
<%= f.submit 'Ok' %>
</p>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment