Rails:对于模型 A has_many B Belongs_to C 返回 A,其中 B 属于 C 并且 B 是最近为给定 A 创建的?
我有三个模型 A、B 和 C,其中 A has_many B
; B 属于 C
,并且 B 有一个created_on 时间戳
。 C has_many A through B。
我想返回all A,其中A有一个属于C的B,并且B是该A最近创建的B。
我可以使用方法循环来做到这一点。这可以仅使用named_scopes来完成吗?还是其他一些优雅/有效的方式?
根据对现实世界 (TM) 的请求,示例 A、B 和 C 可以想象为宠物 (A)、宠物名称 (B) 和名称 (C)。宠物名称是在特定时间赋予宠物的,任何给定的名称都可以赋予许多宠物。任何宠物都可以有多个名称(:通过关系)。对于给定的名称,我想要所有个宠物,其中该名称是该宠物最近创建的 PetName。该调用可能类似于 @name.pets.most_recently_named_as
I have three models A, B, and C, where A has_many B
; B belongs to C
, and B has a created_on timestamp
. C has_many A through B.
I want to return all A where A has a B that belongs to C and where B is the most recently created B for that A.
I can do this with a method loop. Can this be done solely with named_scopes?, or is some other elegant/efficient manner?
As per request for real world (TM) examples A, B and C can be imagined as, for instance, Pets (A), PetsName (B), and Names (C). PetsNames are given to Pets at a specific time, and any given Name can be given to many Pets. Any Pet can have many Names (a :through relationship). For a given Name I want all of the Pets in which that Name is the most recently created PetName for that Pet. The call may look something like @name.pets.most_recently_named_as
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Rails 的方法是在 pets 上指定一个命名范围。
像这样的事情:
为了保持事物以名称为中心,您始终可以在 C/Name 上定义一个调用命名范围的方法。
你总是可以这样做
,
这是一个相当复杂的连接。这表明您可能应该重新考虑存储这些信息的方式。为什么名称位于单独的表中如此重要?
您最好这样做:将
name
和old_name
列添加到 Pet 后:The Rails way to do this is a named scope on pets.
Something like this:
To keep things name centric you could always define a method on C/Name that invokes the named scope.
You could always do
And
It's a pretty complicated join. Which is an indicator that you should probably should rethink the way you store this information. Why is it so important that Names be in a separate table?
You might be better off doing something like this:
After adding
name
andold_name
columns to Pet: