Rails has_many 3 个表之间的关联
我有 3 个类:用户、自行车、交易。
一个用户可以拥有多辆自行车,而自行车只有一个用户(车主)。 交易有一辆自行车和一个用户(买家)...
在我的用户模型中,我有这些关联:
has_many :bicycles_owned, :class_name => "Bicycle",
:uniq => true, :foreign_key => "owner_id"
has_many :trans_bicycles_bought, :class_name => "Transaction",
:foreign_key => "buyer_id"
has_many :bicycles_bought, :class_name=> "Bicycle",
:through => :trans_bicycles_bought, :source => :bicycle
has_many :trans_bicycles_sold, :class_name => "Transaction",
:through => :bicycles_owned, :source => :transaction
现在我想要关联 bicycles_sold
...我已经尝试了很多东西,但是我无法获得正确的代码..正确的参数...
I have 3 classes: User, Bicycle, Transaction.
A user can have many bicycles, and bicycle have only one user (owner).
The transaction have one bicycle, and one user (the buyer)...
In my user model I have these associations:
has_many :bicycles_owned, :class_name => "Bicycle",
:uniq => true, :foreign_key => "owner_id"
has_many :trans_bicycles_bought, :class_name => "Transaction",
:foreign_key => "buyer_id"
has_many :bicycles_bought, :class_name=> "Bicycle",
:through => :trans_bicycles_bought, :source => :bicycle
has_many :trans_bicycles_sold, :class_name => "Transaction",
:through => :bicycles_owned, :source => :transaction
Now I want the association bicycles_sold
... I already tried a lot of things, but I can't get the right code.. the right arguments...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
怎么样:
How about:
这里是您可能感兴趣的一篇文章。
Here is an article you might be interested in.