具有第三列的 Rails 分配表
我目前在 Rails 3 中有一个分配表的模型,如下所示(当然还有 sale 和 FinanceCompany 模型):
class SaleFinanceCompany < ActiveRecord::Base
attr_accessible :sale_id, :financeCompany_id, :financedBalance
belongs_to :sale
belongs_to :financeCompany
end
我的问题很简单:如何设置 sale/financeCompany 模型,以便我可以访问相关的财务余额?
例如,在我看来,我希望:
<% for financeCo in sale.financeCompanies %>
<%= "£" + financeCo.financedBalance + " from "%>
<%= financeCo.name %>
<% end %>
不幸的是,这不起作用,错误在于FinishedBalance部分。我能看到建立我的财务公司模型的唯一方法是使用 a
has_many :financedBalances, :through => :saleFinanceCompanies
但这将为我每次销售提供几个财务余额,但我需要一个(每个财务余额都与分配表中的销售和财务公司相关联,因此当我应该能够执行 sale.financeCompany.financedBalance
时,执行 sale.financedBalances.where 等
似乎没有必要。
有什么建议吗?
I currently have a model for an assignment table in Rails 3, which looks as follows (there are, of course, sale and financeCompany models also):
class SaleFinanceCompany < ActiveRecord::Base
attr_accessible :sale_id, :financeCompany_id, :financedBalance
belongs_to :sale
belongs_to :financeCompany
end
My question is simple: how can I set up the sale/financeCompany models so that I can access the associated financedBalance?
For example, in my view I would like to have:
<% for financeCo in sale.financeCompanies %>
<%= "£" + financeCo.financedBalance + " from "%>
<%= financeCo.name %>
<% end %>
That unfortunately does not work, with the error being the financedBalance part. The only way I could see to set up my finance company model would be with a
has_many :financedBalances, :through => :saleFinanceCompanies
but this will give me several financedBalances for each sale, but I need one (each financedBalance is tied to both a sale and finance company in the assignment table, so doing sale.financedBalances.where etc.
would seem unnecessary when I should be able to do sale.financeCompany.financedBalance
).
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Rails 对待连接表的方式与您想象的有些不同。从 DBA 的角度来看,您的联接表完全没问题,但对于 Rails 来说,真正的联接表只有引用列。一旦添加新列,Rails 就会将连接表视为新实体。
(就我个人而言,一开始我对此感到沮丧,但我很快就意识到这没什么大不了的)
因此,要解决您的问题,您需要重命名您的表,比方说 FinanceBalances。另外,让我们将FinishedBalance 更改为amount。
然后,在您的 Sale.rb 文件中进行如下所示的关联:
对 FinanceCompany 执行相同的操作。
您的代码将如下所示:
如果您确实希望
financeCompany.financedBalance
正常工作,您可以在 FinanceCompany 模型中定义一个方法并编写返回您想要的查询。Rails treats join tables a bit differently than you might think. From a DBA perspective, your join table is perfectly fine but for Rails true join tables have only referential columns. As soon as you add a new column, Rails likes to treat the join table as a new entity.
(On a personal note, I was frustrated by this at first but I quickly learned it's not a big deal)
So, to fix your problem, you'll need to rename your table, let's say FinanceBalances. Also, let's change financedBalance to amount.
Then, in your Sale.rb file put you associations like so:
Do the same for FinanceCompany.
And your code will look like:
If you really really want
financeCompany.financedBalance
to work, you can define a method in your financeCompany model and write the query that returns what you want.