Ruby on rails 轨道4+;太阳黑子:搜索用户拥有的东西;“喜欢”;根据它们的属性(多态性通过关联有很多)
我在Rails 4应用程序中有如下内容:Ruby on rails 轨道4+;太阳黑子:搜索用户拥有的东西;“喜欢”;根据它们的属性(多态性通过关联有很多),ruby-on-rails,full-text-search,polymorphic-associations,sunspot,Ruby On Rails,Full Text Search,Polymorphic Associations,Sunspot,我在Rails 4应用程序中有如下内容: class Like < ActiveRecord::Base belongs_to :liker, class_name: 'User' belongs_to :likeable, polymorphic: true end class Photo < ActiveRecord::Base # has a `title` and `description` attributes has_many :likes, as: l
class Like < ActiveRecord::Base
belongs_to :liker, class_name: 'User'
belongs_to :likeable, polymorphic: true
end
class Photo < ActiveRecord::Base
# has a `title` and `description` attributes
has_many :likes, as: likeable
has_many :likers, through: :likes
end
class Song < ActiveRecord::Base
# has a `title` and `description` attributes
has_many :likes, as: likeable
has_many :likers, through: :likes
end
class Video < ActiveRecord::Base
# has a `title` and `description` attributes
has_many :likes, as: likeable
has_many :likers, through: :likes
end
class User < ActiveRecord::Base
has_many :likes, source: :liker
has_many :liked_photos, through: :likes, source: :likeable, source_type: 'Photo'
has_many :liked_videos, through: :likes, source: :likeable, source_type: 'Video'
has_many :liked_songs, through: :likes, source: :likeable, source_type: 'Song'
end
但后来我弄不清楚如何根据当前用户的喜好确定范围。还是应该在类似的模型上进行搜索?例如:
@search = Like.search { fulltext params[:term] }
searchable
方法中应该包含哪些内容,以及这些内容最适合哪些模型?以下是我最后要做的:
class Like < ActiveRecord::Base
searchable do
text :likeable_title do
likeable.title
end
text :likeable_description do
likeable.description
end
integer :liker_id
end
end
然后,我可以使用likeable
为每个结果渲染视图。如果您有更好的方法,或者有什么我没有考虑的,请告诉我。如果您不介意,请您回顾一下我与您的问题相关的问题,好吗@LearningROR,在过去的几年里,我没有真正与Rails或Sunspot合作过,所以在这一点上我可能不是一个好的资源。
class Like < ActiveRecord::Base
searchable do
text :likeable_title do
likeable.title
end
text :likeable_description do
likeable.description
end
integer :liker_id
end
end
@search = Like.search(include: :likeable) do
fulltext params[:term]
with :liker_id, current_user.id
end