0

我想获取张贴谁去了同一所大学作为当前用户的这些用户的所有帖子......所以我欢迎控制器里面我写了下面的代码..如何在控制器内使用行动数组变量


class WelcomesController < ApplicationController 

def index 

    @col = Education.select(:college_id).where(:user_id => @current_user) 
    @user = Education.select(:user_id).where(:college_id => @col) 
    @welcome = Welcome.where(:user_id => @user) 

end 

end 

以下为欢迎和教育模式我的玛代码:

create_table "welcomes", :force => true do |t| 
    t.text  "message" 
    t.integer "user_id" 
end 

create_table "educations", :force => true do |t| 
    t.integer "college_id" 
    t.integer "user_id" 
end 

@col = Education.select(:college_id).where(:user_id => @current_user) .... 此行返回与当前登录的user.This相关高校IDS是我的控制台被返回下面的输出上可以正常使用..

[#<Education college_id: 1>, #<Education college_id: 2>] 

但我不知道如何在我的下一行使用这个输出,所以我写了这个声明,它应该返回所有用户的大学ID是prevous语句的输出

@user = Education.select(:user_id).where(:college_id => @col) 

和我的最后一行应该返回发表其ID是@user阵列内的用户的所有帖子:

@welcome = Welcome.where(:user_id => @user) 

但这不是working.When我跑我的项目,我看不到我的网页和控制台上的任何输出我越来越接着输出: SELECT welcomes。* FROM welcomes WHERE(welcomesuser_id IN(NULL)) 这意味着它没有得到任何的用户ID .. 我怎么能解决这个...

+0

**使用粗体文本会使事情变得更难以阅读,并且不知道什么是重要的,所以请避免使用粗体文本处理长句,而是使用粗体文本强调真正需要强调的内容。 ** – Zabba 2012-04-09 06:33:04

+0

好吧Zabba会照顾你说的... – NJF 2012-04-09 07:31:44

回答

0

你可以试试这个:

@col = Education.select(:college_id).where(:user_id => @current_user.id).all 
@users = Education.select(:user_id).where(:college_id => @col.collect(&:college_id)).all 
@welcome = Welcome.where(:user_id => @users.collect(&:user_id)).all 
+0

Zabba:是的! – okodo 2012-04-09 06:38:42

+0

非常感谢... user1297959 ....我的答案解决了我的查询... – NJF 2012-04-09 07:16:32

0

我看到完成的最好方法这是为了在用户和教育模型之间建立has_many_and_belongs_to_many关系。 (每个教育将有许多用户,每个用户可能有多个教育)。您需要在数据库中创建一个加入表以支持这种类型的关系 - 有关详细信息,请参见Rails Guide

我会成立自己的模型这种方式:

class User < ActiveRecord::Base 
    has_one :welcome 
    has_and_belongs_to_many :educations 
end 

class Education < ActiveRecord::Base 
    has_and_belongs_to_many :users 
end 

class Welcome < ActiveRecord::Base 
    belongs_to :user 
end 

连接表的has_many_and_belongs_to_many连接表迁移(一定要仔细检查这个代码,不知道我这完全正确):

def self.up 
    create_table 'education_user', :id => false do |t| 
    t.column :education_id, :integer 
    t.column :user_id, :integer 
    end 
end 

控制器代码现在更简单,看起来像这样:

@welcomes = @current_user.eductions.users.welcome.all 

您认为:

<% @welcomes.each do |welcome| %> 
    <p><%= welcome.message %></p> 
<% end %> 

之一的Ruby on Rails的更强大的功能是模型的关系。他们在前面做了一些工作,但如果你花时间正确地设置它们,他们可以让你的生活变得更容易,正如上面简化的@welcomes查询所证明的那样。

0

我建议你做用户和拼贴

class User < ActiveRecord::Base 
    has_many :educations 
    has_many :colleges, :through => :educations 
    has_many :posts 

    scope :by_college_id, lambda {|cid| where("exists (select educations.id from educations where educations.user_id = users.id AND educations.college_id in (?) limit 1)", Array.wrap(cid)) } 

    def college_mates 
    self.class.by_college_id(self.college_ids).where("users.id != ?", id) 
    end :through => :educations 
end 

class Education < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :college 
end 

所以现在的关系在你的控制器,你可以写

class WelcomesController < ApplicationController 
    def index 
    @posts = @current_user.college_mates.includes(:posts).map(&:posts).flatten 
    # or 
    @posts = Post.where(user_id: @current_user.college_mates.map(&:id)) 
    end 
end 

第二个变体产生3 SQL的请求,第一个变体 - 仅二。但是这与数据一样,我认为时间也会一样。通常控制器只包含几行代码,所有的逻辑都写在模型中。理想情况下,控制器应该只包含Post.by_college_mates_for(@curren_user.id)