Guest User

Untitled

a guest
Aug 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. rails many to many association with foreign key
  2. # text.rb
  3. class Text < ActiveRecord::Base
  4. has_many :text_variables, :foreign_key => :text_sid, :primary_key => :sid
  5. has_many :variables, :through => :text_variables
  6. end
  7.  
  8. # text_variable.rb
  9. class TextVariable < ActiveRecord::Base
  10. belongs_to :text, :foreign_key => :text_sid
  11. belongs_to :variable
  12. end
  13.  
  14. # variable.rb
  15. class Variable < ActiveRecord::Base
  16. has_many :text_variables
  17. has_many :texts, :through => :text_variables
  18. end
  19.  
  20. <!-- index.html -->
  21. <% for text in @texts %>
  22. <h2><%= text.name %></h2>
  23. <div name="<%= text.sid %>">
  24. <%= form_for text, :url => "" do |f| %>
  25. <%= f.hidden_field :id, { :value => text.id } %>
  26. <% for vari in @varis %>
  27. <%= check_box_tag :variable_ids, vari.id, text.variables.include?(vari), :name => 'text[variable_ids][]', :style => 'width:auto' -%>
  28. <%= label_tag :variable_ids, vari.name -%><br />
  29. <% end %>
  30. <%= f.submit "Opslaan", :class => "submit", :style => "float:none;" %>
  31. <% end %>
  32. </div>
  33. <% end %>
Add Comment
Please, Sign In to add comment