Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails 3 (3.1) ActiveModel Associations (tableless nested models)

How to impliment ActiveModel associations (tableless nested models)?

For example:

book has many chapters

With ActiveRecord I would create two models and assosiate them with has_many and belongs_to. But ActiveModel doesn't have such functionality. How can I implement this?

like image 293
WHITECOLOR Avatar asked Jul 06 '11 08:07

WHITECOLOR


2 Answers

With rails versions >= 2.3.x you can use the activerecord-tableless gem. With that gem you can have associations and validations without a database.

Update

I have been added as author to the gem and I have updated the gem to support newer Rails versions. So now we can have tableless models with associations in Rails versions >= 2.3

like image 177
Jarl Avatar answered Nov 03 '22 21:11

Jarl


You simply can't do it that way. It is not active record.

You can check ActiveModel documentation (and source code) at :

https://github.com/rails/rails/tree/master/activemodel

I guess you have to do it old fashion way, using an array of chapters and a reference to the book in the chapters.

Hope this helps!

like image 22
Dominic Goulet Avatar answered Nov 03 '22 21:11

Dominic Goulet