【问题标题】:How to get not only single record but all records that belong to specific search query如何不仅获取单个记录,还获取属于特定搜索查询的所有记录
【发布时间】:2013-10-26 12:01:48
【问题描述】:

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

 @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"> 

虽然在我的消息模型中,我有两条记录,发件人用户名为“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:string

正文:字符串

读取:布尔值

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

【问题讨论】:

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


    【解决方案1】:

    Rails 通过称为ActiveRecord 的技术使用relational databases。这是一种 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

    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 一个associated model,这意味着你可以区分是管理员还是用户发送了消息。

    这意味着如果你调用@messages = @user.received_messages,ActiveRecord会自动pingMessage model,并拉出receiveable_id = user.idreceiveable_type = User所在的行


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

    sendable_id
    sendable_type
    
    receiveable_id
    receiveable_type
    

    【讨论】:

    • 感谢您的回答,但我认为问题在于我不仅有一个用户模型,还有一个应该能够相互通信的管理模型! SO ids 不起作用,但在我的两个模型中,用户名是独一无二的!也许你想编辑你的答案谢谢
    • 你的意思是像支持票系统管理员可以向用户等发送消息?
    • 嗯,那你可能想让关系多态!这就是 ActiveRecord 真正令人兴奋的地方。让我编辑我的答案
    【解决方案2】:

    find_by_sender_username 将始终只返回匹配的第一条记录。

    您应该使用where 来获取所有记录。

    @messages = Message.where(sender_username: @user.username)
    

    【讨论】:

    • 我试过Message.where_sender_username(@user.username),但也只返回了一条记录!但是现在有了你的代码就可以了
    • 你能告诉我如何获取用户和收件人用户名(elias)的消息吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多