ruby-on-rails - 如何不仅获取单个记录,还获取属于特定搜索查询的所有记录

标签 ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

我编写了这个搜索查询,它只返回一条记录:

 @messages = Message.find_by_sender_username(@user.username)

 #<Message id: 1, sender_username: "emmanuel", reciepent_username: "elias", body: "Die erste Nachricht", gelesen: "", created_at: "2013-10-26 11:17:53", updated_at: "2013-10-26 11:17:53"> 

尽管在我的消息模型中,我有两 strip 有 sender_username“emmanuel”的记录

 #<Message id: 1, sender_username: "emmanuel", reciepent_username: "elias", body: "Die erste Nachricht", gelesen: "", created_at: "2013-10-26 11:17:53", updated_at: "2013-10-26 11:17:53"> 
 #<Message id: 2, sender_username: "emmanuel", reciepent_username: "vera", body: "Was soll ich dazu sagen", gelesen: "", created_at: "2013-10-26 11:23:57", updated_at: "2013-10-26 11:23:57">

我认为问题是模型之间没有真正的关系:

管理员 用户名:字符串

用户 用户名:字符串

消息

发件人用户名:字符串

reciepent_username:字符串

正文:字符串

读取: bool 值

MyQuestion 是我应该如何编写这些模型之间的关系,我为这种关系准备了一些关于 forgein_key 的内容,但我不是 100% 确定如何使用它!

最佳答案

Rails 使用 relational databases通过一项名为 ActiveRecord 的技术。这是一种 SQL 类型,基本上允许您通过一系列关系“链接”表:

belongs_to
has_one
has_many
has_many :through
has_one :through
has_and_belongs_to_many

链接表的方式取决于关系的构建方式,但在您的情况下,您将使用如下所示的内容:

Ruby on rails - Reference the same model twice?

#app/models/message.rb
has_one :sender, :class_name => 'User', :foreign_key => 'sender_id'
has_one :recipient, :class_name => 'User', :foreign_key => 'recipient_id'


#app/models/user.rb
has_many :sent_messages, :class_name => 'Message', :foreign_key => 'sender_id'
has_many :received_messages, :class_name => 'Message', :foreign_key => 'recipient_id'

您必须编辑表以使每个关系都有一个外键列。您已经完成了此操作,但您需要知道外键始终是一个ID。因此,您将拥有 sender_id

,而不是 sender_username

这是因为主键始终是唯一的ID,这意味着ID可以用来标识该记录在其他表中的关联;这意味着您将能够在执行的任何查询中引用该记录


这将允许您执行以下操作:

@messages = @user.received_messages

不必费力处理 100 个不同的查询


更新

Polymorphic Association

如果您想将管理消息包含在其中,则需要包含 polymorphic association :

class Message < ActiveRecord::Base
  belongs_to :sendable, polymorphic: true
  belongs_to :receiveable, polymorphic: true
end

class Admin < ActiveRecord::Base
  has_many :sent_messages, :class_name => 'Message', as: :sendable
  has_many :received_messages, :class_name => 'Message', as: :receiveable
end

class User < ActiveRecord::Base
  has_many :sent_messages, :class_name => 'Message', as: :sendable
  has_many :received_messages, :class_name => 'Message', as: :receiveable
end

我不知道这段代码是否可以开箱即用,但它绝对为您指明了正确的方向。它的工作方式是为您的 foreign_key 提供一个关联模型,这意味着您可以区分是管理员还是用户发送了消息。

这意味着,如果您调用 @messages = @user.received_messages ,ActiveRecord 将自动 ping 消息模型,并提取 receiveable_id = 的行user.id AND receiveable_type = User


为此,您需要将 sender_username 和receiver_username 替换为 4 列:

sendable_id
sendable_type

receiveable_id
receiveable_type

关于ruby-on-rails - 如何不仅获取单个记录,还获取属于特定搜索查询的所有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19606459/

相关文章:

ruby-on-rails - 缓存 : [GET/] miss? dalli gem,memcached,rails 3.1,nginx, unicorn 生产环境

ruby-on-rails-4 - 如何将 payumoney 支付网关与 Rails 4 集成

ruby-on-rails - 如何在 Ruby 中快速连接两个字符串

ruby-on-rails - RSpec 失败(未定义方法 `id' 为 nil :NilClass ) with has_many through model

ruby-on-rails - 将Bootswatch主题与Twitter-Bootstrap-Rails Gem集成

ruby-on-rails - 为什么当 rails redirect_to 成功调用新的 url 时浏览器的地址没有改变?

css - 我在 RAILS 4.2.4 元素上尝试将背景 url 图像单独绑定(bind)到我的 home.html.erb 时遇到困难

ruby-on-rails - Cucumber 和 webrat - 如何处理 paths.rb 中的动态 URL?

ruby-on-rails - Rails 3 - 时间比较?

ruby-on-rails - 克隆新创建的记录时忽略验证