Last active
August 29, 2015 14:03
-
-
Save armstrongnate/4e94e38c04e86a6990ad to your computer and use it in GitHub Desktop.
example of a many to many relationship in rails
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
class Post < ActiveRecord::Base | |
has_many :post_tags | |
has_many :tags, through: :post_tags | |
end |
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
class PostTag < ActiveRecord::Base | |
belongs_to :post | |
belongs_to :tag | |
end |
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
<!-- app/views/posts/_form.html.erb --> | |
<%= form_for @post do |f| %> | |
<div class='row'> | |
<div class='col-md-6'> | |
<div class='form-group'> | |
<%= f.label :title, class: 'form-label' %> | |
<%= f.text_field :title, class: 'form-control' %> | |
</div> | |
</div> | |
<div class='col-md-6'> | |
<div class='form-group'> | |
<%= f.label :tags, class: 'form-label' %> | |
<%= f.select :tag_ids, options_from_collection_for_select(Tag.all, 'id', 'name'), {}, { multiple: true, class: 'form-control' } %> | |
</div> | |
</div> | |
<div class='col-md-12'> | |
<div class='form-group'> | |
<%= f.label :content, class: 'form-label' %> | |
<%= f.text_area :content, class: 'form-control', rows: 20 %> | |
</div> | |
</div> | |
<div class='col-md-12'> | |
<%= f.submit 'Submit', class: 'btn btn-primary' %> | |
</div> | |
</div> | |
<% end %> |
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
<!-- app/views/posts/index.html.erb --> | |
<h1>Posts</h1> | |
<div class='row'> | |
<div class='col-md-7'> | |
<% @posts.each do |post| %> | |
<div class='post'> | |
<h2><%= post.title %></h2> | |
<hr> | |
<div> | |
<%= truncate(post.content, length: 200) %> | |
</div> | |
<% if post.tags.any? %> | |
<div class='tags'> | |
<strong>Tags:</strong> | |
<% post.tags.each do |tag| %> | |
<%= link_to tag do %> | |
<span class='label label-default'><%= tag.name %></span> | |
<% end %> | |
<% end %> | |
</div> | |
<% end %> | |
</div> | |
<% end %> | |
</div> | |
<div class='col-md-5'> | |
<%= link_to 'New Post', new_post_path, class: 'btn btn-warning pull-right' %> | |
</div> | |
</div> |
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
class PostsController < ApplicationController | |
respond_to :html | |
def index | |
@posts = Post.all | |
end | |
def new | |
@post = Post.new | |
end | |
def create | |
@post = Post.create(safe_params) | |
respond_with @post | |
end | |
private | |
def safe_params | |
safe_attributes = [ | |
:title, | |
:content, | |
tag_ids: [], | |
] | |
params.require(:post).permit(*safe_attributes) | |
end | |
end |
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
bin/rails g model Post title content:text | |
bin/rails g model Tag name | |
bin/rails g model PostTag post:belongs_to tag:belongs_to | |
bin/rake db:migrate |
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
<!-- app/views/tags/show.html.erb --> | |
<h1><%= "#{@tag.name.pluralize} Posts" %></h1> | |
<ul> | |
<% @tag.posts.each do |post| %> | |
<li><%= link_to post.title, post %></li> | |
<% end %> | |
</ul> |
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
class Tag < ActiveRecord::Base | |
has_many :post_tags | |
has_many :posts, through: :post_tags | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment