sachin god
sachin god

Reputation: 685

How to build relations in rails?

I am having 3 tables:

  1. User which consists of name, email, password, confirm_password, role as fields.
  2. Timesheet which consists of date, time, hours_worked as fields.
  3. Attachment consists of upload_file field. This upload_file field is used in Timesheet form.

Now how can I build relation in all scenarios, for example I want to view the upload_file of an user.

I am new to rails. Please help. Thanks in advance.

Upvotes: 1

Views: 33

Answers (1)

argentum47
argentum47

Reputation: 2382

Well, try to think of what you want with your app in english first, and ORM later. Lets say You want User to have multiple Timesheet and each TimeSheet to have one Attachment. So you can say

class User < AR::Base
  has_many :timesheets
end

class TimeSheet < AR::Base
  has_one :attachment
end

and your database fields need some extra parameters, timesheets need user_id and attachments need timesheet_id.

Hence I think, that you should also learn a bit about databases while learning rails, and RTFM

Upvotes: 2

Related Questions