如何显示 has_many 关系中的一项?如何对对象进行更改,然后在 has_many 中创建一个新项目?
我试图仅显示和捕获 *has_many* 关系中的“最新”项目。基本上,我想找到存在于 *has_many* 关系中的单个对象(基于某些条件)并将其与记录一起显示。
我的类关系定义如下(精简):
class Member < ActiveRecord::Base
has_many :member_status_histories
has_many :member_statuses, :through => :member_status_history
end
class MemberStatusHistory < ActiveRecord::Base
belongs_to :member_status
belongs_to :member
has_one :timespan
end
class MemberStatus < ActiveRecord::Base
attr_accessible :name
has_many :memberStatusHistories
has_many :members, :through => :memberStatusHistories
end
class Timespan < ActiveRecord::Base
attr_accessible :startDate, :endDate
belongs_to :timespanable, :polymorphic => true
end
特别是使用上面的类和关系,我希望有一个页面显示成员对象和一个成员状态历史记录 没有结束日期的对象。如果用户更改页面上的会员状态历史记录字段,我想获取该信息并创建一个新的会员状态历史记录对象,并将其保存到member_status_histories中会员。
我已经尝试了几件事,但考虑到我是 RoR 新手,我不确定使用什么方法来做到这一点。似乎有几种方法可以尝试让它发挥作用,但我无法让其中任何一种完全发挥作用。
对我来说最有意义的想法是使用虚拟属性。虚拟属性将提供我想要显示的会员状态历史记录对象,然后当用户提交更新的会员和会员状态历史记录时,我可以在虚拟属性上运行一些业务逻辑(使用 before_save)并根据需要更新 *member_status_histories* 字段。
同样,我无法找到任何工作方法,因此我愿意接受有关如何实现这一目标的建议。谢谢!
I am trying to display and capture only the "most recent" item from a *has_many* relationship. Basically, I want to find a single object that exists in a *has_many* relationship (based on some criteria) and display that with the record.
I have the class relationships defined like so (stripped down):
class Member < ActiveRecord::Base
has_many :member_status_histories
has_many :member_statuses, :through => :member_status_history
end
class MemberStatusHistory < ActiveRecord::Base
belongs_to :member_status
belongs_to :member
has_one :timespan
end
class MemberStatus < ActiveRecord::Base
attr_accessible :name
has_many :memberStatusHistories
has_many :members, :through => :memberStatusHistories
end
class Timespan < ActiveRecord::Base
attr_accessible :startDate, :endDate
belongs_to :timespanable, :polymorphic => true
end
Specifically using the classes and relationships above, I want to have a page that displays a member object and the one member status history object that does not have an end date. If the user changes the fields on the page for the member status history, I want to take that information and make a new member status history object and save that to the member_status_histories in member.
I have tried several things, but granted that I'm new to RoR, I'm not sure what approach to use to do this. It seems like there are several ways to try to get this to possibly work, but I haven't been able to get any of them to fully work.
The idea that seemed to make the most sense to me was using a virtual attribute. The virtual attribute would provide the member status history object that I wanted to display, and then when the user submitted the updated member and member status history, I could run some business logic on the virtual attribute (using before_save) and update the *member_status_histories* field as needed.
Again, I have been unable to get any approach to work so I am open to suggestions on how to accomplish this. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会放弃多个 MemberStatusHistories 的概念。它很快就会变得非常复杂,幸运的是,已经有一个 gem 可以完成您想要的一切,称为 paper_trail。在member_status_history.rb中:
然后你可以执行@member.member_status_history.previous_version来查看当前member_status_history之前的版本,并且当前版本始终是@member.member_status_history中的“live”版本。这应该允许您始终查看“最新”成员状态历史记录,同时始终能够从 paper_trail 版本表中获取任意数量的剩余成员。
有关其工作原理的更多信息,请查看 paper_trail github 文档。
I'd ditch the concept of multiple MemberStatusHistories. It'll quickly get very complicated and luckily for you there's already a gem that does everything you want called paper_trail. In member_status_history.rb:
Then you can do @member.member_status_history.previous_version to see the version before the current member_status_history, and the current one is always the "live" one at @member.member_status_history. That should allow you to always see the "most recent" member status history while constantly having the ability to an arbitrary number of remaining ones from your paper_trail version table.
For more information on how that all works, check out the paper_trail github documentation.