如何不仅获取单个记录而且获取属于特定搜索查询的所有记录 [英] How to get not only single record but all records that belong to specific search query

查看:57
本文介绍了如何不仅获取单个记录而且获取属于特定搜索查询的所有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 @messages = Message.find_by_sender_username(@user.username)#<消息 ID:1,sender_username:emmanuel",reciepent_username:elias",正文:Die erste Nachricht",gelesen:",created_at:2013-10-26 11:17:53",updated_at:2013-10-26 11:17:53">

虽然在我的 Message 模型中,我有两条记录,其 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">#<消息ID:2,sender_username:emmanuel",reciepent_username:vera",body:Was soll ich dazu sagen",gelesen:",created_at:2013-10-26 11:23:57",更新时间:2013-10-26 11:23:57">

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

管理员用户名:字符串

用户用户名:字符串

留言

sender_username:string

reciepent_username:string

正文:字符串

读:布尔值

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

解决方案

Rails 使用 :

类消息'消息',如::可发送has_many :received_messages, :class_name =>'消息',如::可接收结尾类用户'消息',如::可发送has_many :received_messages, :class_name =>'消息',如::可接收结尾

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

这意味着如果你调用 @messages = @user.received_messages ,ActiveRecord 会自动 ping Message model,并拉出 receiveable_id =user.id AND receiveable_type = User

<小时>

为此,您需要替换您的 sender_username &4 列的接收者用户名:

sendable_id可发送类型可接收ID可接收类型

I wrote this search query, that only returns an single record:

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

Although in my Message model i have two records with the 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">

I think the problem is that i have no real relation between the models:

Admin username:string

User username:string

Message

sender_username:string

reciepent_username:string

body:string

read:boolean

MyQuestion is how i should write a relation between this models, i herad something about forgein_key for such relations but im not 100% sure how to make use of it!

解决方案

Rails uses relational databases through a technology called ActiveRecord. This is a type of SQL which basically allows you to "link" tables through a series of relations:

belongs_to
has_one
has_many
has_many :through
has_one :through
has_and_belongs_to_many

The way you link your tables is dependant on how the relationship is built, but in your case, you'd use something like this:

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'


You'll have to edit your tables to have a foreign key column for each relation. You've kind-of done this already, but you need to know that a foreign key is always an ID. So instead of sender_username, you'd have sender_id

This is because the primary key is always a unique ID, which means that ID can be used to identify the record's associations in other tables; meaning you'll be able to reference the record in any query you perform


This will allow you to do this:

@messages = @user.received_messages

Instead of having to mess around with 100's of different queries


Update

If you want to include Admin messages into the mix, you'd want to include a 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

I don't know if this code will work out of the box, but it is definitely point you in the right direction. The way it works is to give your foreign_key an associated model, which means that you can differentiate between whether an admin or user sent the message.

This means that if you call @messages = @user.received_messages , ActiveRecord will automatically ping the Message model, and pull out the rows where receiveable_id = user.id AND receiveable_type = User


For this to work, you'll need to replace your sender_username & receiver_username with 4 columns:

sendable_id
sendable_type

receiveable_id
receiveable_type

这篇关于如何不仅获取单个记录而且获取属于特定搜索查询的所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆