ruby-on-rails – Rails 3:用户对实体的多态喜好,怎么样?
|
背景:
我按照教程here在我的应用程序中设置了多态用户收藏夹数据模型.这允许我让用户在系统中创建几乎任何实体,我添加’has_many:favorites,:as => :有利’的线条,其模型的最爱.我计划使用它来实现Facebook风格的“Like”系统以及其他几个类似的系统. 首先,我将优先级添加到Post模型(每个用户都可以在Facebook上创建状态更新).我完成了所有这些工作并进行了单元测试,因此我知道数据模型在关系的任何一方(User和Post)都是健全的. 细节: >我有一个带有单索引方法和视图的Home控制器. 我被卡住了 >如何添加链接或按钮以将帖子添加到用户的收藏夹? 在此先感谢您的帮助! 解决方法
Jaap,感谢您对我的问题的评论.在写完这个问题之后,我几乎不想等待,因为真正的学习是通过反复试验来实现的,所以我把它弄错了;)
事实证明,你所建议的几乎与我最终做的事情完全一致(我总是很高兴发现你决定做的是其他人会做的事情,我喜欢它的理智检查价值). 所以这就是我所做的,而且它都是通过回复来完成的.现在我只需要实现AJAX并设置样式: 我最喜欢的模型因为我的Polymorphic Favorites模型要求实体只能被我添加到验证“范围”的用户一次收藏,这表明对于每个属性,它必须在其他2个必需属性的范围内是唯一的.这解决了同一用户的多个收藏夹的问题. class Favorite < ActiveRecord::Base
before_save :associate_user
belongs_to :favorable
belongs_to :user
# Validations
validates :user_id,:presence => true,:uniqueness => {:scope => [:favorable_id,:favorable_type],:message => "item is already in favorites list."}
validates :favorable_id,:uniqueness => {:scope => [:user_id,:message => "item is already in favorites list."}
validates :favorable_type,:user_id],:message => "item is already in favorites list."}
# Callbacks
protected
def associate_user
unless self.user_id
return self.user_id = session[:user_id] if session[:user_id]
return false
end
end
end
我的用户模型(相关的):我添加了2个方法,get_favorites与教程中的有利方法和收藏夹相同?检查有问题的实体是否已添加到用户的收藏夹的方法. class User < ActiveRecord::Base
# Relationships
has_many :microposts,:dependent => :destroy
has_many :favorites
# Methods
def favorite?(id,type)
if get_favorites({:id => id,:type => type}).length > 0
return true
end
return false
end
def get_favorites(opts={})
# Polymorphic Favoritability: allows any model in the
# application to be favorited by the user.
# favorable_type
type = opts[:type] ? opts[:type] : :topic
type = type.to_s.capitalize
# add favorable_id to condition if id is provided
con = ["user_id = ? AND favorable_type = ?",self.id,type]
# append favorable id to the query if an :id is passed as an option into the
# function,and then append that id as a string to the "con" Array
if opts[:id]
con[0] += " AND favorable_id = ?"
con << opts[:id].to_s
end
# Return all Favorite objects matching the above conditions
favs = Favorite.all(:conditions => con)
case opts[:delve]
when nil,false,:false
return favs
when true,:true
# get a list of all favorited object ids
fav_ids = favs.collect{|f| f.favorable_id.to_s}
if fav_ids.size > 0
# turn the Capitalized favorable_type into an actual class Constant
type_class = type.constantize
# build a query that only selects
query = []
fav_ids.size.times do
query << "id = ?"
end
type_conditions = [query.join(" AND ")] + fav_ids
return type_class.all(:conditions => type_conditions)
else
return []
end
end
end
end
我的Micropost模型(相关的):注意has_many关系中的Polymorphic关联标题为:favorites. class Micropost < ActiveRecord::Base
attr_accessible :content
# Scopes
default_scope :order => 'microposts.created_at DESC'
# Relationships
belongs_to :user
has_many :favorites,:as => :favorable # Polymorphic Association
# Validations
validates :content,:length => { :minimum => 1,:maximum => 140 }
validates :user_id,:presence => true
end
我的Micropost表单:正如您所看到的那样,我将作为本地变量映射到Favorite模型的实体传递给2 Favorite表单作为’local_entity’.这样我就可以为多态关联提取ID和实体的TYPE. <div class="post">
<span class="value">
<%= micropost.content %>
</span>
<span>
<% if current_user.favorite?(micropost.id,micropost.class.to_s) %>
<%= render :partial => 'favorites/remove_favorite',:locals => {:local_entity => micropost} %>
<% else %>
<%= render :partial => 'favorites/make_favorite',:locals => {:local_entity => micropost} %>
<% end %>
</span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<div class="clear"></div>
</div>
我最喜欢的表格: <%= form_for current_user.favorites.build do |f| %>
<div><%= f.hidden_field :favorable_id,:value => local_entity.id %></div>
<div><%= f.hidden_field :favorable_type,:value => local_entity.class.to_s %></div>
<div class="actions"><%= f.submit "make favorite" %></div>
<% end %>
我删除最喜欢的表格: <%= form_for current_user.get_favorites(
{:id => local_entity.id,:type => local_entity.class.to_s}),:html => { :method => :delete } do |f| %>
<div class="actions"><%= f.submit "remove favorite" %></div>
<% end %>
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
