我有3个表 - 用户,事物和以下.用户可以通过下表跟踪事物,将a user_id与a 关联things_id.这意味着:
class User
has_many :things, :through => :follows
end
class Thing
has_many :users, :through => :follows
end
class Follow
belongs_to :users
belongs_to :things
end
Run Code Online (Sandbox Code Playgroud)
所以我可以毫无问题地检索thing.users.我的问题是如果在下面的表中,我有一个名为"relation"的列,所以我可以将一个关注者设置为"admin",我希望能够访问该关系.所以在循环中我可以做类似的事情:
<% things.users.each do |user| %>
<%= user.relation %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
有没有办法将关系包含在原始用户对象中?我试过了:select => "follows.relation",但它似乎没有加入该属性.
在对模型和数据库更改方法/函数进行单元测试时,单元测试的最佳方法或思维方式是什么?例如,模型中的"发布"函数除了通过/失败之外没有可测试的行为,并且在传递的情况下,它修改数据库.最佳做法或方法?
目前的想法是在测试之前创建当前数据库的镜像,只需更改单元测试文件中的数据库选择.谢谢你的建议.