Rails data modelling with has_many through

35 views
Skip to first unread message

Pradeep Achuthan

unread,
Aug 31, 2015, 10:47:26 AM8/31/15
to Ruby on Rails: Talk
Hi,

I am building an expense tracker application and I am in middle of data modelling. I have a Users table. Each user will log his expenses with expense type and income with income type. So I need to know how can we set up associations for it.

I have User, Expense, Income and UserTransaction models.

Expense and Income will have following fields

 id, date, category, amount, description, user_id, currency

I am not sure whether I need UserTransaction table also.
But my business requirement is as follows

I should be able to get all expenses/income of a user with date range and also with category

I should also be able to get all transactions occurred with date range.

Colin Law

unread,
Aug 31, 2015, 11:30:33 AM8/31/15
to Ruby on Rails: Talk
On 31 August 2015 at 15:47, Pradeep Achuthan <pradeep...@gmail.com> wrote:

I notice that the last time you asked basically the same question you
did not appear to reply or offer thanks for the help offered, which is
not good manners, unless I missed the post. However I will try to
help again. See below,

> Hi,
>
> I am building an expense tracker application and I am in middle of data
> modelling. I have a Users table. Each user will log his expenses with
> expense type and income with income type. So I need to know how can we set
> up associations for it.
>
> I have User, Expense, Income and UserTransaction models.
>
> Expense and Income will have following fields
>
> id, date, category, amount, description, user_id, currency

There is a big clue there. If two tables have the same fields then
almost certainly it should just be one table. Possibly call it
transactions and use different categories to distinguish between
income and expenses.

>
> I am not sure whether I need UserTransaction table also.
> But my business requirement is as follows
>
> I should be able to get all expenses/income of a user with date range and
> also with category
>
> I should also be able to get all transactions occurred with date range.

I also don't see any need for a UserTransaction table. I think you
just need User has_many transactions, Transaction belongs_to user.

Colin

prabhu

unread,
Aug 31, 2015, 1:29:14 PM8/31/15
to Ruby on Rails: Talk
Hi Colin,

First of all Sorry for neglecting the previous comments. I didnt notice that i had reply for the previous posts which I posted. Thats the reason I posted it once again here.
Thanks for your suggestions.

BTW I was thinking whether polymorphic association would really be nice for my scenario

John Lahr

unread,
Aug 31, 2015, 2:12:14 PM8/31/15
to Ruby on Rails: Talk
I would look into the concept of a single table inheritance for a transactions table...you'd have to add an extra attribute/column that would be 'transaction' type to indicate whether it's an expense or income, but this should be able to be done with a simple 1 to many relationship - a user has many transactions - Single Table Inheritance is great for things like this and Rails handles it very easily.

Colin Law

unread,
Aug 31, 2015, 2:48:36 PM8/31/15
to Ruby on Rails: Talk
On 31 August 2015 at 18:29, prabhu <pradeep....@gmail.com> wrote:
> Hi Colin,
>
> First of all Sorry for neglecting the previous comments. I didnt notice that
> i had reply for the previous posts which I posted. Thats the reason I posted
> it once again here.
> Thanks for your suggestions.
>
> BTW I was thinking whether polymorphic association would really be nice for
> my scenario

Why? What advantage would that have over the simple setup I suggested?

Colin

>
> On Monday, 31 August 2015 20:17:26 UTC+5:30, Pradeep Achuthan wrote:
>>
>> Hi,
>>
>> I am building an expense tracker application and I am in middle of data
>> modelling. I have a Users table. Each user will log his expenses with
>> expense type and income with income type. So I need to know how can we set
>> up associations for it.
>>
>> I have User, Expense, Income and UserTransaction models.
>>
>> Expense and Income will have following fields
>>
>> id, date, category, amount, description, user_id, currency
>>
>> I am not sure whether I need UserTransaction table also.
>> But my business requirement is as follows
>>
>> I should be able to get all expenses/income of a user with date range and
>> also with category
>>
>> I should also be able to get all transactions occurred with date range.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/f4439ded-0ae6-4f4f-b85b-499ab02c8450%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Colin Law

unread,
Aug 31, 2015, 2:51:11 PM8/31/15
to Ruby on Rails: Talk
On 31 August 2015 at 19:12, John Lahr <john...@gmail.com> wrote:
> I would look into the concept of a single table inheritance for a
> transactions table...you'd have to add an extra attribute/column that would
> be 'transaction' type to indicate whether it's an expense or income, but
> this should be able to be done with a simple 1 to many relationship - a user
> has many transactions - Single Table Inheritance is great for things like
> this and Rails handles it very easily.

Whether the added complication of STI is worth the benefits in this
case depends on what code differences there are between income and
expenses. I would start off with the simple solution and if it
becomes apparent that STI would be beneficial then refactor it at that
point.

Colin

>
> On Monday, August 31, 2015 at 7:47:26 AM UTC-7, Pradeep Achuthan wrote:
>>
>> Hi,
>>
>> I am building an expense tracker application and I am in middle of data
>> modelling. I have a Users table. Each user will log his expenses with
>> expense type and income with income type. So I need to know how can we set
>> up associations for it.
>>
>> I have User, Expense, Income and UserTransaction models.
>>
>> Expense and Income will have following fields
>>
>> id, date, category, amount, description, user_id, currency
>>
>> I am not sure whether I need UserTransaction table also.
>> But my business requirement is as follows
>>
>> I should be able to get all expenses/income of a user with date range and
>> also with category
>>
>> I should also be able to get all transactions occurred with date range.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/b17f15cb-fbe5-47c8-be72-c369ba8ae0d6%40googlegroups.com.

Colin Law

unread,
Aug 31, 2015, 4:45:13 PM8/31/15
to Ruby on Rails: Talk
On 31 August 2015 at 18:29, prabhu <pradeep....@gmail.com> wrote:
> Hi Colin,
>
> First of all Sorry for neglecting the previous comments. I didnt notice that
> i had reply for the previous posts which I posted. Thats the reason I posted
> it once again here.
> Thanks for your suggestions.

I notice, looking back through your previous posts over more than a
year you have rarely responded to replies here.

Colin
Message has been deleted

jel...@gmail.com

unread,
Aug 31, 2015, 10:34:32 PM8/31/15
to rubyonra...@googlegroups.com
It would be nice if this thread got back on topic with the OPs problem. We are all here to help. 

Sent from my iPhone

On Aug 31, 2015, at 8:14 PM, Elizabeth McGurty <emcg...@gmail.com> wrote:

Dear Colin,

I think that what you are broaching is an ethical matter in the use of this Talk Group.  Generally folks cheating, as students or incompetent employees.

Where and when does one choose not to help.

Liz

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages