【问题标题】:how to find records from another table in rails?如何从rails中的另一个表中查找记录?
【发布时间】:2012-04-03 06:08:23
【问题描述】:

我有欢迎、用户、学院、教育模型.. 在欢迎页面上,我只想显示那些与当前用户上同一所大学的用户发布的消息……但我无法显示。我在欢迎控制器中编写了以下代码:

def index
  @welcome = Welcome.all
  @newmessage = Welcome.new
  @college = User.find(@current_user.id).colleges
end

使用第一行,我可以在我的视图中显示所有用户发布的消息,但这不是我想要的。使用第三行,我可以找到当前用户的大学数据,但我不确定我该怎么做使用这些数据...

我怎样才能只搜索那些与当前用户具有相同大学的用户...

以下是型号代码:


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

class College < ActiveRecord::Base
    has_many :educations
    has_many :users, :through => :educations
end

class Welcome < ActiveRecord::Base
    belongs_to :user
end

以下是这些模型的架构代码:

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

create_table "users", :force => true do |t|
    t.string   "email",                                 :default => "", :null => false
    t.string   "encrypted_password",     :limit => 128, :default => "", :null => false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",                         :default => 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "first_name"
    t.string   "last_name"
    t.integer  "role_id"
    t.string   "ancestry"
  end
create_table "educations", :force => true do |t|
    t.integer  "college_id"
    t.integer  "user_id"
    t.integer  "year"
    t.text     "concentration"
    t.string   "attended_for"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "month"
    t.boolean  "private",         :default => false
    t.boolean  "approved",        :default => false
    t.integer  "approved_by"
    t.boolean  "alumni",          :default => false
    t.boolean  "is_mentor",       :default => false
    t.boolean  "approved_mentor", :default => false
  end

create_table "colleges", :force => true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.boolean  "approved",   :default => false
  end

【问题讨论】:

    标签: ruby-on-rails find rails-activerecord


    【解决方案1】:

    看看这是否有帮助 -

    User.find(@current_user.id).colleges.joins(:users => :welcomes).select('welcomes.message')
    

    【讨论】:

    • 你能看到 User.find(@current_user.id).colleges.joins(:user).select('user.email as email').pluck(:email) 的输出吗跨度>
    • sairam...我收到以下错误..ActiveRecord::ConfigurationError: 未找到名为“用户”的关联;也许你拼错了?
    • 非常抱歉 - 它的用户。 User.find(@current_user.id).colleges.joins(:users).select('user.email as email').pluck(:email)
    • pluck 是在 rails 3.2 或后期 rails 3.1 中引入的。我假设您使用的是最新的导轨。忽略 pluck 并遍历返回的数组。
    • 不用循环,用".map(&:email)"代替"pluck"
    猜你喜欢
    • 2010-09-26
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多