-
-
Save zhulinpinyu/a5123d4f0abae1246270 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
rails g scaffold Book title:string description:text | |
rails g scaffold Pubhouse title:string address:text | |
rails g model BookPubhouse book_id:integer pubhouse_id:integer | |
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/models/book.rb' | |
class Book < ActiveRecord::Base | |
has_many :book_pubhouses, :dependent => :destroy # уничтожать вместе с основным экземпляром | |
has_many :pubhouses, :through => :book_pubhouses # through указывает Rails приложению зависимость двух моделей через третью, теперь мы можем использовать Book.pubhouses напрямую | |
# валидация | |
validates :title, :presence => true | |
validates :title, :uniqueness => true | |
validates :title, :length => { :maximum => 150 } | |
validates :description, :length => { :maximum => 2000 } | |
end | |
# 'app/models/pubhouse.rb' | |
class Pubhouse < ActiveRecord::Base | |
has_many :book_pubhouses, :dependent => :destroy | |
has_many :shops, :through => :book_pubhouses | |
validates :title, :presence => true | |
validates :title, :uniqueness => true | |
validates :title, :length => { :maximum => 150 } | |
validates :address, :length => { :maximum => 2000 } | |
end | |
# 'app/models/book_pubhouse.rb' | |
class BookPubhouse < ActiveRecord::Base | |
belongs_to :book | |
belongs_to :pubhouse | |
validates :book_id, :presence => true | |
validates :pubhouse_id, :presence => true | |
validates :pubhouse_id, :uniqueness => {:scope => :book_id} | |
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
<%= form_for(@book) do |f| %> | |
... | |
<div class="param"> | |
<%= f.label :pubhouses %> | |
<span class="ul"> | |
<% Pubhouse.all.each do |pubhouse| %> | |
<label class="li"><%= check_box_tag :pubhouse_ids, pubhouse.id, @book.pubhouses.include?(pubhouse), :name => 'book[pubhouse_ids][]' %> <u><%= pubhouse.title %></u></label><br /> | |
<% end %> | |
</span> | |
</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
<%= form_for(@book) do |f| %> | |
... | |
<div class="param"> | |
<%= f.label :pubhouses %> | |
<%= select_tag("book[pubhouse_ids][]", options_for_select(Pubhouse.all.collect { |pubhouse| [pubhouse.title, pubhouse.id] }, @book.pubhouses.collect { |pubhouse| pubhouse.id}), {:multiple=>true, :size=>5}) %> | |
</div> | |
... | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment