我有一個簡單的應用程序與用戶模型,角色模型,與收件人的消息模型。 用戶通過角色 發送了很多消息,角色通過收件人發送了很多消息,並且用戶還通過收件人發送了很多消息。如何連接Rails中的兩個ActiveRecord查詢
收件人模型具有USER_ID,ROLE_ID和MESSAGE_ID
,現在我想顯示包括他的角色的消息和在一個查詢他的個人信息的用戶的所有郵件。我怎樣才能做到這一點。
role model
has_many :recipients
has_many :received_messages, :class_name => 'Message', :order => 'created_at desc', through: :recipients
user model
has_many :roles
has_many :received_messages, :class_name => 'Message', :order => 'created_at desc', through: :roles, :uniq => true
has_many :recipients
has_many :private_messages, :class_name => 'Message', :order => 'created_at desc', through: :recipients
message model
has_many :recipients
has_many :roles, :through => :recipients
has_many :users, :through => :recipients
所有這些在地方我要去如何獲取用戶的消息都他的角色的消息,他的私人信息
例如把所有的消息是
def all_messages
@all_messages = current_user.#all messages scope here#
end
你有has_many:用戶模型的角色。這意味着角色模型右邊有一個belongs_to:用戶? – jvnill 2013-03-10 01:09:36