Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rails many to many association with foreign key
- # text.rb
- class Text < ActiveRecord::Base
- has_many :text_variables, :foreign_key => :text_sid, :primary_key => :sid
- has_many :variables, :through => :text_variables
- end
- # text_variable.rb
- class TextVariable < ActiveRecord::Base
- belongs_to :text, :foreign_key => :text_sid
- belongs_to :variable
- end
- # variable.rb
- class Variable < ActiveRecord::Base
- has_many :text_variables
- has_many :texts, :through => :text_variables
- end
- <!-- index.html -->
- <% for text in @texts %>
- <h2><%= text.name %></h2>
- <div name="<%= text.sid %>">
- <%= form_for text, :url => "" do |f| %>
- <%= f.hidden_field :id, { :value => text.id } %>
- <% for vari in @varis %>
- <%= check_box_tag :variable_ids, vari.id, text.variables.include?(vari), :name => 'text[variable_ids][]', :style => 'width:auto' -%>
- <%= label_tag :variable_ids, vari.name -%><br />
- <% end %>
- <%= f.submit "Opslaan", :class => "submit", :style => "float:none;" %>
- <% end %>
- </div>
- <% end %>
Add Comment
Please, Sign In to add comment