如何使用Rails定义与当前用户具有相同元素的用户?

时间:2016-02-18 08:42:17

标签: ruby-on-rails ruby

我尝试根据新用户选择的类别制作向新用户显示推荐用户的功能。

因此推荐用户应为user_type:" star"并且它具有相同的category.id,即current_user被选中。

这些是模特。

class User < ActiveRecord::Base
 has_many :categorizings, as: :categorizable, dependent: :destroy
 has_many :categories,    through: :categorizings
end

class Categorizing < ActiveRecord::Base
 belongs_to :category
 belongs_to :categorizable, polymorphic: true
end


class Category < ActiveRecord::Base
 has_many :categorizings, dependent: :destroy
 has_many :users, through: :categorizings, source: :categorizable, source_type: 'User'
end

并且,这是我的伪编码方法,以找到我坚持使用的推荐用户...

CategoriesController < ApplicationController
 def recommended_user
  @recommended_user = User.where(user_type: 'star' && Who has same category.id with a current_user)
 end
end

对于令人困惑的解释感到抱歉,但有任何帮助。 谢谢!

2 个答案:

答案 0 :(得分:1)

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Get Previous next Day, Month, Year Month Time in Angularjs</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('sampleapp', [])
app.controller('samplecontrol', function ($scope) {
 
var previousMonth = new Date()
$scope.pmonth = previousMonth.getMonth();
var currentMonth = new Date()
$scope.cmonth = currentMonth.getMonth() + 1;
var nextMonth = new Date()
$scope.nmonth = nextMonth.getMonth() + 2;
 
});
</script>
</head>
<body data-ng-app="sampleapp" data-ng-controller="samplecontrol">
<form id="form1">
<div>
 
Previous Month:<b> {{pmonth }}</b><br />
Current Month:<b> {{cmonth }}</b><br /> 
Next Month:<b> {{nmonth }}</b><br /> 
</div>
</form>
</body>
</html>

答案 1 :(得分:0)

@recommended_user = User.where(user_type: 'star').where(category_id: current_user.category_id)