Mam i ktoś może dodać swoich krewnych do drzewa.Jak elegancko obsługiwać dwustronne relacje z tłumaczeniami Rails?
Co się dzieje, jeśli dla każdego wpisu family_tree utworzono rekord membership
.
Jednak jeśli Son
dodaje Dad
, powinniśmy być w stanie zaktualizować drzewo genealogiczne taty, aby dodać "syna" do drzewa w widoku. Jaka jest najlepsza metoda Railsów do podejścia do tego? Wiem, że Rails wykonuje wiele tłumaczeń natywnie, i pluralizacje itp. W każdym razie, żeby wykorzystać to do tego, co chcę zrobić?
Co to jest klasa/moduł, który ponownie obsługuje te rzeczy? ActiveSupport?
To mój User
model:
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# email :string(255) default(""), not null
# encrypted_password :string(255) default(""), not null
# reset_password_token :string(255)
# reset_password_sent_at :datetime
# remember_created_at :datetime
# sign_in_count :integer default(0), not null
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
# created_at :datetime
# updated_at :datetime
# name :string(255)
# confirmation_token :string(255)
# confirmed_at :datetime
# confirmation_sent_at :datetime
# unconfirmed_email :string(255)
# invitation_relation :string(255)
# avatar :string(255)
#
class User < ActiveRecord::Base
has_one :family_tree, dependent: :destroy
has_many :memberships, dependent: :destroy
has_many :nodes, dependent: :destroy
has_many :participants, dependent: :destroy
end
FamilyTree.rb
# == Schema Information
#
# Table name: family_trees
#
# id :integer not null, primary key
# name :string(255)
# user_id :integer
# created_at :datetime
# updated_at :datetime
#
class FamilyTree < ActiveRecord::Base
belongs_to :user
has_many :memberships, dependent: :destroy
has_many :members, through: :memberships, source: :user, dependent: :destroy
has_many :nodes, dependent: :destroy
end
Membership.rb
:
# == Schema Information
#
# Table name: memberships
#
# id :integer not null, primary key
# family_tree_id :integer
# user_id :integer
# created_at :datetime
# updated_at :datetime
# relation :string(255)
#
class Membership < ActiveRecord::Base
belongs_to :family_tree
belongs_to :user
end
Node.rb
# == Schema Information
#
# Table name: nodes
#
# id :integer not null, primary key
# name :string(255)
# family_tree_id :integer
# user_id :integer
# media_id :integer
# media_type :string(255)
# created_at :datetime
# updated_at :datetime
# circa :datetime
# is_comment :boolean
#
class Node < ActiveRecord::Base
belongs_to :family_tree
belongs_to :user
belongs_to :media, polymorphic: true, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :node_comments, dependent: :destroy
end
My _tree.html.erb
wygląda następująco (obcięty dla zwięzłość):
<li class="tree-item-name"><a href="#">Great Grandparents</a>
<ul>
<li><% if relative.humanize == "Great Grandfather" || relative.humanize == "Great Grandmother" %>
<%= link_to image_tag(membership.user.avatar.url, size: "48x48", :class => "img-circle") , family_tree_path(membership.user.family_tree), :target => '_blank' %>
<%= link_to membership.user.name, family_tree_path(membership.user.family_tree), :target => '_blank'%>
<% else %>
None added yet, add them <%= link_to 'here', "#" , class: 'btn invite popupbox','data-popup' => 'invite_friend' %>
<% end %>
</li>
</ul>
</li>
<li class="tree-item-name"><a href="#">Grandparents</a>
<ul>
<li><% if relative.humanize == "Grandfather" || relative.humanize == "Grandmother" %>
<%= link_to image_tag(membership.user.avatar.url, size: "48x48", :class => "img-circle") , family_tree_path(membership.user.family_tree), :target => '_blank' %>
<%= link_to membership.user.name, family_tree_path(membership.user.family_tree), :target => '_blank' %>
<% else %>
None added yet, add them <%= link_to 'here', "#" , class: 'btn invite popupbox','data-popup' => 'invite_friend' %>
<% end %>
</li>
</ul>
</li>
Mógłbyś: usunąć cały kod, który nie jest istotny (Devise, attr_accessible, Carrierwave, inne powiązania, itp.); wyjaśnić, w jaki sposób tworzone są nowe relacje rodzinne (jaka jest rola tokena zaproszenia?). – tompave
Również, jeśli to możliwe i stosowne, abstrakcja z dala od szczegółów niskiego poziomu jako tokenów zaproszeń. Zadaj pytanie tylko o skojarzeniach. – tompave
Ostatnia rzecz: wspomniałeś _tłumaczenia_. Co masz na myśli? Zdolność Railsów do odmiany nazw skojarzeń? Czy chcesz wiedzieć o tym, aby zautomatyzować generowanie ciągów w widoku? – tompave