Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 是否存在activerecord关系来解决此问题?_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails 是否存在activerecord关系来解决此问题?

Ruby on rails 是否存在activerecord关系来解决此问题?,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我似乎无法对这件事保持清醒。我有三张桌子: mysql> desc users; +----------------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------------------+--------------+------+

我似乎无法对这件事保持清醒。我有三张桌子:

mysql> desc users;
+----------------------+--------------+------+-----+---------+----------------+
| Field                | Type         | Null | Key | Default | Extra          |
+----------------------+--------------+------+-----+---------+----------------+
| id                   | int(11)      | NO   | PRI | NULL    | auto_increment |
| username             | varchar(255) | YES  |     | NULL    |                |
+----------------------+--------------+------+-----+---------+----------------+

mysql> desc mentions;
+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| id               | int(11)      | NO   | PRI | NULL    | auto_increment |
| message_id       | int(11)      | YES  |     | NULL    |                |
| mentionable_type | varchar(255) | YES  |     | NULL    |                |
| mentionable_id   | int(11)      | YES  |     | NULL    |                |
+------------------+--------------+------+-----+---------+----------------+

mysql> desc messages;
+------------+----------+------+-----+---------+----------------+
| Field      | Type     | Null | Key | Default | Extra          |
+------------+----------+------+-----+---------+----------------+
| id         | int(11)  | NO   | PRI | NULL    | auto_increment |
| body       | text     | YES  |     | NULL    |                |
| user_id    | int(11)  | YES  |     | NULL    |                |
+------------+----------+------+-----+---------+----------------+
以及以下关系:

class User < ActiveRecord::Base
  has_many :messages
end

class Message < ActiveRecord::Base
  belongs_to :user
  has_many :mentions
end

class Mention < ActiveRecord::Base
  belongs_to :mentionable, :polymorphic => true
  belongs_to :message
end
与tweet类似,消息正文可能包含,也可能不包含n个用户或组的提及。当消息“我正在与@userA、@userB和@groupX共进午餐”被创建时,正文将被解析,这三个“提及”也将被创建

我可以通过以下方式轻松返回用户的所有“提及”:

如果我想看到提及的信息,我可以:

mention = current_user.mentions.first
mention.message

我看不出来的是一种干净的方法,可以将两者结合起来,得到用户创建并在中提到的消息列表。有什么想法吗?

在您的用户模型中,这一行应该用于多态关系

class User
  has_many :messages
  has_many :mentions, :as => :mentionable
end
试试这个:

user_id = 10
@messages = Message.find(:all, :joins => [:mentions], 
                         :conditions => ['messages.user_id = ?', user_id])

实际的查询需要执行以下操作:从messages.id=indications.message\u id where indications.indicatable\u id=或messages.user\u id=上留下的消息中选择messages.*或者如果您使用的是rails3,则使用
message.joins(:indications)。其中(:user\u id=>user\u id)
,如果数据库中出现不明确的列名错误,您可能需要将
where
子句更改为
.where([“messages.user\u id=?”,user\u id])
。不是100%确定rails如何处理中的哈希语法。这正是我需要的。多谢。
class User
  has_many :messages
  has_many :mentions, :as => :mentionable
end
user_id = 10
@messages = Message.find(:all, :joins => [:mentions], 
                         :conditions => ['messages.user_id = ?', user_id])