Created
September 9, 2009 19:01
-
-
Save cmilfont/183980 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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