Forum: Ruby on Rails Copy fields from association

Posted by masta Blasta (mastablasta)
on 2013-02-18 23:51
I have a model Report, and a model ReportComponent. ReportComponent
belongs to Report, and Report has_many components. Both tables have some
commons fields such as organization_id, manager_id etc, and of course
their own unique fields. There is no hard business reason for having
these duplicate fields, but they help with join queries and DB
performance.

The issue i have is that I would like for ReportComponent to
automatically pull organization_id from Report on creation, and store
that in its own field. Not the same as 'AR#delegate' as this would copy
and
write the field.

Best idea i have right now is to override the organization= method, but
that is not foolproof. Is there a native rails method for this?

Thanks,
Posted by Colin Law (Guest)
on 2013-02-19 08:55
(Received via mailing list)
On 18 February 2013 22:51, masta Blasta <lists@ruby-forum.com> wrote:
> I have a model Report, and a model ReportComponent. ReportComponent
> belongs to Report, and Report has_many components. Both tables have some
> commons fields such as organization_id, manager_id etc, and of course
> their own unique fields. There is no hard business reason for having
> these duplicate fields, but they help with join queries and DB
> performance.

Don't repeat the fields, that is not good.  Can you explain why you
need them?  There will almost certainly be a better way.

Colin
Posted by masta Blasta (mastablasta)
on 2013-02-19 15:39
Colin Law wrote in post #1097732:
> On 18 February 2013 22:51, masta Blasta <lists@ruby-forum.com> wrote:
>> I have a model Report, and a model ReportComponent. ReportComponent
>> belongs to Report, and Report has_many components. Both tables have some
>> commons fields such as organization_id, manager_id etc, and of course
>> their own unique fields. There is no hard business reason for having
>> these duplicate fields, but they help with join queries and DB
>> performance.
>
> Don't repeat the fields, that is not good.  Can you explain why you
> need them?  There will almost certainly be a better way.
>
> Colin

Most commonly is to make our jobs easier when analyzing the DB data. 
JOIN queries become a bit simpler. Yes it slightly defeats the purpose 
of a relational database, but we haven't had any issues so far. Just the 
convenience of having those variables available right away is enough to 
bend some of the rules.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.