问题标签 [belongs-to]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
ruby-on-rails - Help with rails belongs_to
I'm stuck -
I'm building an accounting app in rails, and have a model where a customer will have multiple invoices as well as multiple payments (related to those invoices)
Here's a quick look at a simplified model:
I have a nested form where I want to show Customer attributes, CustomerPayment attributes and CustomerPaymentItem attributes - which all works fine.
I also want to show Invoice attributes for each CustomerPaymentItem (each CustomerPaymentItem relates back to a single invoice) and while I can get a form to show the CustomerPaymentItem info, I can't get it to show Invoice information which is needed to give reference to the user. - I'm having trouble getting data from a belongs_to association to show on the form.
I'm at a loss - Shouldn't I be able to traverse the belongs_to association? FYI - I can send data to the log where I know the invoice data is populated during the CustomerPayment.new call, it seems to just get lost between the controller and the form.
How should I access that data? Here's the form info -- (coming from a couple of rendered forms) the stuff that doesn't show is in between the ---.
Am I missing something in my fields_for to show the :invoice reference data? Is my model too complex for rails to make sense of?
ruby-on-rails - Rspec - 尽管用户无效(has_many/belongs_to 关联),但respond_to“用户帖子”仍然有效
我不了解与 rspec 的测试关联(has_many / belongs_to)相当基本的东西。
模型中的关联:
我设置了一个没有属性的用户并测试一个帖子是否应该响应用户。测试有效,但用户无效(并且未创建)。
我知道 respond_to 仅通过关联测试帖子的存在,但如果没有有效用户,它怎么可能存在?有人可以解释我为什么吗?谢谢!
user_spec.rb
cakephp - 每个`belongTo`文件上传和每个文件上传的多个表可以在模型中没有关系吗?
我正在使用文件上传表(字段:)id, name, size, type
并使用一系列其他表(注释、文件),每个表都有一个外键指向文件上传表(字段:)中的一条记录upload_id
。每个系列表(例如,注释、文件)的模型将包括指向文件上传表的belongTo 关系。
note_id
这意味着文件上传表将没有外键(多个表指向它),因此除非我为每个其他表(例如,,filing_id
等)都有一个字段,否则外来 id 字段将不起作用。该模型不会提及任何关系。
我应该用另一种方式吗?我想不出更有效的方法。
grails - Grails GORM自引用belongsTo删除与预期相反的方向
我有一个 Grails 域类,它是类别的层次结构。每个类别都有一个父类别(根类别除外,它为空)。
我的问题:删除与我期望的完全相反的级联:
- someSubCategory.delete() 删除类别,然后尝试删除父类别(如果父类别有其他子类别,则会失败并违反完整性)。
- parentCategory.delete() 不会级联删除其子级,而是会因违反完整性而失败。
我究竟做错了什么?我的理解是,上面的“belongsTo”应该告诉 GORM 级联删除从父级到所有子级,而不是从子级到其父级。
ruby-on-rails - 当它的超类是抽象的时,为什么 Rails 不尊重与 STI 关联的 belongs_to 关联对象的类型?
我在我正在开发的 Rails 应用程序中遇到了这种相当奇怪的行为。
我在继承层次结构中有多种类型的 Post,并且 Post has_many FeedEntries。
现在,当我像以前一样设置好所有东西时,对保存的对象调用 FeedEntry#post 总是返回一个正确(子类)类型的对象,正如我所期望的那样。但是,如果我使 Post 抽象(它确实应该是 - 超类永远不应该在此模型中实例化):
_(注意:我编辑了此代码片段以考虑到以下 tomafro 的建议,因为设置 self.abstract_class 似乎比覆盖 self.abstract_class 更惯用?但是,同样的行为仍然存在。)
...然后在以前保存的对象上调用 FeedEntry#post 关联会返回 Post 类型的对象。这似乎相当倒退(鉴于抽象类声明明确表示不应实例化该类),我想不出这种行为的原因。
那么,我没有得到什么原因,或者它是一个错误,还是其他什么?
ruby-on-rails - Rails has_one 和 belongs_to 帮助
我有两个模型:用户和商店
用户必须登录才能通过输入“email”和“store_name”来注册商店。从stores_controller 创建看起来像这样:
在应用程序控制器中
但是,当我签入数据库时,@store.user_id = nil。由于某种原因,它无法将 current_user.id 放入 @store.user_id。任何人都可以帮助检测为什么会这样?我以为我已经正确实施了关联。谢谢
ruby-on-rails - 在rails中为has_many关系指定默认记录的最佳方法
我有帐户和帐户地址。一个帐户可以有多个 AccountAddressess,我想指定一个作为“default_account_address”,因此在 Account 表中,我有一个名为“default_account_address_id”的列。这是模型的当前状态。
除了 @account.default_account_address 返回一个帐户地址和 @account.account_addresses 返回一个空数组之外,这很好用。
所以,问题是@account.account_addresses 中不包含默认帐户地址。
有关解决此问题的最佳方法的任何想法?我考虑过habtm,但似乎不合适。我考虑使用 has_one :default_account_address,但这没有意义,因为 default_account_address_id 列在帐户表上。谢谢。
ruby-on-rails-3 - 具有belongs_to 和has_many 关系的脚手架:“Edit”和“Destroy”的link_to 路径不起作用
我的项目允许一个家庭页面有多个故事。模型包括关系——“家庭有_很多故事”和“故事属于家庭”。在 routes.rb 文件中,我有以下内容:
导致故事控制器的以下路由:
以下是相关的控制器方法:
对于 index.html.erb,nifty 脚手架生成的代码没有考虑关系,并且“Show”、“Edit”和“Destroy”的链接不起作用。经过一番研究,我修改了这些链接的代码,如下所示:
当用“[@family,story]”替换原来的“story”变量时,“Show”链接可以正常工作。“销毁”和“编辑”链接不适用于类似的替换。
“Destroy”链接不会产生任何错误,但就像“Show”链接一样 - 记录不会被删除,而是会显示(:confirm 对话框从不显示。)“Edit”链接会生成以下错误:
[@family,story] 结构适用于“显示”链接。为什么它不适用于“编辑”和“销毁”链接?如何修改它们以使其正常工作?
ruby-on-rails - 三个模型的多个belongs_to关系
情况是这样的。。
问题是我如何填充角色成员表中的所有三个外键..当我执行 org.users.push(u) 时,这会创建一条记录,但 role_id 被忽略了......
ruby-on-rails - Rails - 使用belongs_to时未生成符号
我有一个按以下方式设置的模型 Tran:
现在,在我看来,我正在尝试访问:submitting_user_id
,因为我已经设置了外键,但是 rails 对我大喊大叫并说它找不到符号。我不应该访问它吗?如果没有,我怎样才能获得访问权限?看法:
错误: