Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

aviral prakash
aviral prakash
518 Points

What should i do to create a table in ruby on rails where a user can add his friends and other attributes

Basically my app is based on treebook app. With each status(a bill in my app) i want to tag friends. Not only tag them but also assign some amount of money paid by them. What should i do create such table associated with every status(bill).

1 Answer

Dan S
Dan S
1,966 Points

Still very new to rails myself, but I think you need to be looking at using belongs_to and has_many in your models. So if you had a model called Users and a model called Bills, then in the Users model, you would have has_many :bills and in the Bills model you would have belongs_to :users. This would create the association.

Another handy way of doing it is when creating the scaffold at the beginning you can do:

rails g scaffold model users name:string phone_number:integer bills:has_many rails g scaffold model bills friend:string amount:integer users:belongs_to

It may not be exactly correct, but this is how I've been doing associations it so far. This link helped me: http://guides.rubyonrails.org/association_basics.html

Hope it helps.