Ruby on rails 有秩序、有限度地采摘
在我的侧边栏中,我显示了新创建的用户配置文件。配置文件Ruby on rails 有秩序、有限度地采摘,ruby-on-rails,sorting,activerecord,pluck,Ruby On Rails,Sorting,Activerecord,Pluck,在我的侧边栏中,我显示了新创建的用户配置文件。配置文件属于用户,用户有一个配置文件。我意识到我只使用了profile表中的3列,所以最好使用pulk。我还有一个链接到用户路径(profile.user),所以我不得不告诉用户是谁。目前我使用的包括,但我不需要整个用户表。因此,我使用来自用户和概要文件表的许多列 我怎样才能用勇气来优化它?我尝试了几个版本,但总是出现一些错误(大多数时候profile.user没有定义) 我当前的代码: def set_sidebar_users @profil
属于用户,用户有一个配置文件。我意识到我只使用了profile表中的3列,所以最好使用pulk。我还有一个链接到用户路径(profile.user)
,所以我不得不告诉用户是谁。目前我使用的包括,但我不需要整个用户表。因此,我使用来自用户和概要文件表的许多列
我怎样才能用勇气来优化它?我尝试了几个版本,但总是出现一些错误(大多数时候profile.user没有定义)
我当前的代码:
def set_sidebar_users
@profiles_sidebar = Profile.order(created_at: :desc).includes(:user).limit(3) if user_signed_in?
end
create_table "profiles", force: :cascade do |t|
t.integer "user_id", null: false
t.string "first_name", null: false
t.string "last_name", null: false
t.string "company", null: false
t.string "job_title", null: false
t.string "phone_number"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
t.string "avatar"
t.string "location"
end
好的,让我们来解释三种不同的方式来完成你所寻找的
首先,在包含和连接
包含与关联的所有指定列的关联。它不允许您从两个表中查询或选择多个列。它是连接所做的事情。它允许您查询两个表并选择您选择的列
def set_sidebar_users
@profiles_sidebar = Profile.select("profiles.first_name,profiles.last_name,profiles.id,users.email as user_email,user_id").joins(:user).order("profile.created_at desc").limit(3) if user_signed_in?
end
它将返回您在select
子句中提供的所有列的Profiles
关系。您可以像对profile objecte-g
@profiles\u侧边栏。首先。user\u email
将向您发送此配置文件的用户电子邮件
如果您想要查询多个表,或者想要从这两个表中选择多个列,那么这种方法是最好的
2.拔毛
def set_sidebar_users
@profiles_sidebar = Profile.order(created_at: :desc).includes(:user).limit(3).pluck("users.email,profiles.first_name") if user_signed_in?
end
Pulk仅用于从多个关联中获取列,但它不允许您使用ActiveRecord
的功能。它只是以相同的顺序返回所选列的数组。
就像在第一个示例中一样,您可以使用@profiles\u侧边栏.first.user
获取用户的profile对象,但使用pulk无法获取,因为它只是一个普通数组。这就是为什么大多数解决方案都会出现错误profile.user未定义的原因
与选定列的关联
现在这是选项三。在第一种解决方案中,您可以在两个表上获得多个列,并使用ActiveRecord
的强大功能,但它并不急于加载关联。因此,如果在返回的结果(如@profiles\u sidebar.map(&:user)
因此,如果您想使用包含
,但想使用选定列,那么您应该与选定列建立新的关联,并调用该关联。
e-g
在profile.rb中
belongs_to :user_with_selected_column,select: "users.email,users.id"
现在您可以将其包含在上面的代码中
def set_sidebar_users
@profiles_sidebar = Profile.order(created_at: :desc).includes(:user_with_selected_column).limit(3) if user_signed_in?
end
现在,这将加载用户,但将只选择电子邮件和用户id。
有关更多信息,请访问
更新
当你问起拔毛的职业选手时,让我们来解释一下。
正如您所知,pluck
返回普通数组。所以它不会实例化ActiveRecord对象,它只是返回从数据库返回的数据。
因此,在不需要ActiveRecord对象,而只是以表格形式显示返回的数据的情况下,最好使用Pulk。
Select将返回这些关系,以便您可以对其进行进一步查询或对其实例调用模型方法。
因此,如果我们总结一下,我们可以说
拾取模型值,选择模型对象
有关更多信息,请访问您是否尝试过类似于Profile.order(创建于::desc)。limit(3)。pull(:'users.name')
?另一个可能的答案是先执行第一个查询(不包括),然后执行@profiles\u侧栏.users.pull(:username)
。我还发现了两条线索可以帮助你回答这个问题:和。我认为你不需要在这里使用勇气。如果您想立即加载关联+仅加载选定列,那么有一种方法可以做到这一点。其次,如果只需要从两个表中选择束,则只需使用联接而不是包含。让我写一个很好的解释,凯撒。还有一个问题:Purch的要点是只返回必要的列。我知道。但是这里的select
似乎也在做同样的事情。那么,与其他人相比,弹拨的竞争优势是什么呢?例如:Profile.select(“profiles.first\u name,profiles.last\u name,profiles.id,users.email as user\u email,user\u id”)
vsProfile.pulk(“profiles.first\u name,profiles.id,users.email as user\u email,user\u id”)
I在答案中添加优缺点,以供将来参考