简体   繁体   English

Ruby on Rails模型关联问题

[英]Ruby on Rails model association issue

Sorry for the vague title, but it's a little much to explain in a sentence. 抱歉,标题含糊,但在句子中有很多解释。

I've got three models, User, Device, and DeviceMessage. 我有三种模型,User,Device和DeviceMessage。 Their relationships are fairly simple: 他们的关系非常简单:

  • a User has_many :devices , 用户has_many :devices
  • a Device belongs_to :user , 设备belongs_to :user
  • a Device has_many :device_messages , 设备has_many :device_messages
  • and a DeviceMessage belongs_to :device . 和一个DeviceMessage当属至belongs_to :device

Rails provides ways to start playing with these associations quickly, like the ability to get all device messages that belong to a certain user (from any device). Rails提供了快速开始处理这些关联的方法,例如能够从任何设备获取属于某个用户的所有设备消息。

In order to do this, I defined a method in the User model: 为此,我在User模型中定义了一个方法:

    class User < ActiveRecord::Base
      ...
      has_many :devices, :as => : owner #Other entities may "own" a device

      def device_feed
        DeviceMessage.that_belong_to_user(self)
      end
    end

And I define the called method in the DeviceMessage model: 然后在DeviceMessage模型中定义被调用的方法:

    class DeviceMessage < ActiveRecord::Base
      ...
      belongs_to :device

      def self.that_belong_to_user(user)
        device_ids = "SELECT owner_id FROM devices WHERE owner_id = :user_id 
                     AND owner_type = \"User\""
        where("device_id IN (#{device_ids})", user_id: user.id)
      end
    end

I define a user page where they can associate a device to their account (the device has a name as well), and upon adding the device to the account, it will add the name to a list of device names in a pane to the left, while showing the user's device feed much like a twitter feed (yes, I followed Michael Hartl's RoR tutorial). 我定义了一个用户页面,他们可以在其中将设备与帐户关联(该设备也具有名称),并将该设备添加到帐户后,它将名称添加到左侧窗格中的设备名称列表中,同时将用户的设备供稿显示为类似于Twitter供稿(是的,我遵循了Michael Hartl的RoR教程)。 At this point it is important to note that I am using helper functions to keep track of the current user so I can display this information when a user visits the root_path while logged in. When visiting the root_path, the controller for the root_path is defined so that: 此时,请务必注意,我正在使用帮助器函数来跟踪当前用户,因此当用户登录时访问root_path时,我可以显示此信息。访问root_path时,已定义root_path的控制器,因此那:

    if user_signed_in?
      @device_feed_items = current_user.device_feed.paginate(page: params[:page])
    end

And this all works perfectly! 这一切都完美地工作!

So... what's the issue? 那么...是什么问题? When I create a new user via the signup page, and associate the device via the device-association page, I am redirected to the root_path, the device name is correctly displayed in the left pane (which mean the device is correctly associated with the new user), but the device_feed is not displayed. 当我创建通过注册页面的新用户,以及通过设备关联页的设备联系起来,我重定向到root_path,设备名称正确显示在左侧窗格中(这意味着该设备被正确的新关联用户),但不会显示device_feed。

I've used the Rails console to verify that the device messages should be showing ( User.find(2).devices.first.device_messages.first displays the first message associated with the first device that is newly associated with the 2nd user), so I know that I need to reach down into the database to get a fresh rather than cached copy of the current_user, but I'm confused because it seems like that should be happening every time the user.device_feed method is called because of it's use of where() which is a part of the query API... 我已使用Rails控制台验证是否显示设备消息( User.find(2).devices.first.device_messages.first显示与第一个与第二个用户新关联的设备相关联的第一条消息),所以我知道我需要深入数据库以获取current_user的新副本而不是缓存的副本,但是我感到困惑,因为似乎每次使用user.device_feed方法时,都应该发生这种情况作为查询API一部分的where() ...

Any ideas? 有任何想法吗? Thanks in advance for any and all answers. 在此先感谢您提供所有答案。

-MM -MM

I am just wondering why you have the device_feed function. 我只是想知道为什么要有device_feed函数。 For your feed display could you not just a loop like this one, this is 对于您的供稿显示,您不仅可以像这样循环播放,这是

class Device < ActiveRecord::Base

  scope :in_new_message_order, :joins => :device_messages, :order => "created_at DESC"      

end

Added a joined scope 添加了合并范围

class User < ActiveRecord::Base
  ...
  has_many :devices, :as => : owner #Other entities may "own" a device
  scope :in_sort_order, order("message_date DESC")

  def device_feed
    DeviceMessage.that_belong_to_user(self)
  end
end

Above I have added a scope to sort your messages 上面我添加了一个范围来对您的消息进行排序

<% user.devices.in_new_message_order.each do |device| %>
    <% device.device_messages_in_sort_order.each do |message| %>
        <%= ....render out the message %>
    <% end %>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM