Proper way to prevent ActiveRecord::ReadOnlyRecord?

后端 未结 4 895
执念已碎
执念已碎 2021-01-03 19:03

I\'m currently using Rails 2.3.9. I understand that specifying the :joins option in a query without an explicit :select automatically makes any re

4条回答
  •  萌比男神i
    2021-01-03 19:25

    Regarding your sub-question: so am I not properly understanding the purpose of automatically setting the read-only flag on :joins?

    I believe the answer is: With a joins query, you're getting back a single record with the User + Subscription table attributes. If you tried to update one of the attributes (say "subscription_num") in the Subscription table instead of the User table, the update statement to the User table wouldn't be able to find subscription_num and would crash. So the join-scopes are read-only by default to prevent that from happening.

    Reference: 1) http://blog.ethanvizitei.com/2009/05/joins-and-namedscopes-in-activerecord.html

提交回复
热议问题