shau-kote
shau-kote

Reputation: 1150

:readonly key of belongs_to method doesn't work

I try to use :readonly key as it described in documentation:

belongs_to :project, readonly: true

That's my code with model associations define:

class Article < ActiveRecord::Base
    belongs_to :feed, inverse_of: :articles, readonly: true
end
class Feed < ActiveRecord::Base
    has_many :articles, inverse_of: :feed, dependent: :delete_all
end

These two snippets look alike. But when I test my models in rails console it doesn't work:

irb(main):001:0> article = Article.first()
ArgumentError: Unknown key: :readonly. Valid keys are: :class_name, :class, :foreign_key, :validate, :autosave, :remote, :dependent, :primary_key, :inverse_of, :foreign_type, :polymorphic, :touch, :counter_cache

I have Rails v. 4.2.0, if it's matter.

What am I doing wrong? Why Rails doesn't interpret this argument?

Thank you.

Upvotes: 0

Views: 661

Answers (2)

Frederick Cheung
Frederick Cheung

Reputation: 84132

The documentation is out of date. The up to date equivalent is

User.belongs_to :feed, -> {readonly}, inverse_of: articles

The lambda is used to build the scope. Anything you can usually use on relations, including readonly, is available here.

Upvotes: 1

usha
usha

Reputation: 29359

This kind of usage of readonly was removed in rails 4.1.2. you will have to use readonly method

eg:

user = User.joins(:todos).select("users.*, todos.title as todos_title").readonly(true).first
user.todos_title = 'clean pet'
user.save! # will raise error

search "readonly" in this changelog for rails 4.1.2

Upvotes: 0

Related Questions