Rails 3.1 构建关联不起作用

发布于 2024-11-19 07:14:40 字数 805 浏览 6 评论 0原文

好的,我有一个方法应该返回传入的包(service_package_id 和 payment_plan_id)并且有效。套餐模型属于服务套餐和付款计划,并且每个模型都有许多套餐。

@service_package = ServicePackage.find(service_package_id)
@payment_plan = PaymentPlan.find(payment_plan_id)   

@package = @service_package.packages.build(:payment_plan_id => @payment_plan, :promo_code => "149", :price => "7.99", :number_of_free_days => "30", :setup_fee => "0", :initial_price => "0.00", :initial_price_duration => "30", :final_price => "0")
logger.error "package #{@package.service_package_id}"
@spackage = @package.service_package 
logger.error "spackage #{@spackage.description}"

一切都很棒,直到 @spackage 行。记录器获取 service_package_id,但当下一个日志发生时,它会返回 nil 类的 nomethoderror。为什么我的@spackage 为零?这适用于 Rails 3.0.9,但至少在我的开发机器上不适用于 3.1 rc4。有什么想法吗?

Ok so I have a method that should be returning a package (service_package_id and payment_plan_id) are passed in and valid. The packages model belongs to both service packages and payment plans and each of those models has many packages.

@service_package = ServicePackage.find(service_package_id)
@payment_plan = PaymentPlan.find(payment_plan_id)   

@package = @service_package.packages.build(:payment_plan_id => @payment_plan, :promo_code => "149", :price => "7.99", :number_of_free_days => "30", :setup_fee => "0", :initial_price => "0.00", :initial_price_duration => "30", :final_price => "0")
logger.error "package #{@package.service_package_id}"
@spackage = @package.service_package 
logger.error "spackage #{@spackage.description}"

Everything is great until the @spackage line. The logger picks up the service_package_id but then when next log occurs it returns with a nomethoderror on nil class. Why is my @spackage nil? This works on rails 3.0.9 but not 3.1 rc4 at least on my dev machine. Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫瑾少女 2024-11-26 07:14:40

想通了。

这里的主要问题是模型没有(故意)访问数据库,但我已经为 payment_plan_id 和 service_package_id 设置了 attr_accessor 方法。由于我将整个对象传递给 id 属性,因此该属性实际上在内存中包含了整个属性。一旦开始访问数据库,构建就应该可以工作

Figured it out.

The main gotcha here was that the model wasn't hitting the database (on purpose) but I had set up attr_accessor methods for payment_plan_id and service_package_id. Since I was passing the whole object in to the id attribute that attribute actually contained the whole attribute in memory. Once it starts hitting the database the build should work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文