# ActiveRecord::ReadOnlyRecord on attributes\_update

**URL:** <https://www.ruby-forum.com/t/activerecord-readonlyrecord-on-attributes-update/184797>\
**Category:** Rails\
**Created:** [March 8, 2010, 9:09pm UTC](https://www.ruby-forum.com/t/activerecord-readonlyrecord-on-attributes-update/184797 "2010-03-08T21:09:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [March 8, 2010, 9:09pm UTC](https://www.ruby-forum.com/t/activerecord-readonlyrecord-on-attributes-update/184797/1 "2010-03-08T21:09:20Z")

</div>

Hi,

I have a ActiveRecord::ReadOnlyRecord error when i update child  
attributes whereas when i create a new one it works fine.

Someone had this error?

Best,  
greg

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [March 8, 2010, 10:28pm UTC](https://www.ruby-forum.com/t/activerecord-readonlyrecord-on-attributes-update/184797/2 "2010-03-08T22:28:07Z")

</div>

On Mar 8, 8:09 pm, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I have a ActiveRecord::ReadOnlyRecord error when i update child  
> attributes whereas when i create a new one it works fine.

find marks records as read only if you specify a :joins option (you  
can override this with :readonly =\> false)

Fred

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [March 8, 2010, 11:37pm UTC](https://www.ruby-forum.com/t/activerecord-readonlyrecord-on-attributes-update/184797/3 "2010-03-08T23:37:51Z")

</div>

Sorry but i’m not sure where do I have to put a join.

here is my code:  
class Product \< ActiveRecord::Base  
attr\_accessible :name, :prod\_type, :min\_visit, :max\_visit, :price,  
:visible, :ingredient\_attributes

has\_and\_belongs\_to\_many :ingredients  
belongs\_to :city\_preferences :city\_preferences

after\_update :save\_ingredients

def ingredient\_attributes=(ingredient\_attributes)  
ingredient\_attributes.each do |ingredient|  
if ingredient[:id].blank?  
ingredients.build(ingredient)  
else  
ing = Ingredient.detect {|i| i.id == ingredient[:id].to\_i}  
ing.attributes = ingredient  
end  
end  
end

def save\_ingredients  
ingredients.each do |t|  
t.save(false) #false parameter = avoid validation  
end  
end

end

class Ingredient \< ActiveRecord::Base  
attr\_accessible :name, :price, :visible

has\_and\_belongs\_to\_many :products  
end

Thx Greg

Frederick C. wrote:

> On Mar 8, 8:09ï¿½pm, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi,
> > 
> > I have a ActiveRecord::ReadOnlyRecord error when i update child  
> > attributes whereas when i create a new one it works fine.
> 
> find marks records as read only if you specify a :joins option (you  
> can override this with :readonly =\> false)
> 
> Fred
