Association to User using a key named approver_id

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Association to User using a key named approver_id

John Sanderbeck
I KNOW this is probably very simple, but I am scratching my head trying to get it to work...

I have a table with a key named approver_id

What I want to do is associate this to the User class

So I can do Event.approver.first_name, etc...

in Event I have

has_one :approver, class: 'User'

So how do I define the relationship to associate Event.approver_id to User.id

John

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bfbf73d2-2cf4-4c52-9896-5b58028f6f3d%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Association to User using a key named approver_id

Niklas Bichinger
Use „class_name“, not „class“ as option key. Also, from the Model that *holds the ID* of the other Model, it’s always „belongs_to“.

So in Event:
belongs_to :approver, class_name: „User“
and you’re good to go.


Sent via iPhone.

Am 26.01.2020 um 16:56 schrieb John Sanderbeck <[hidden email]>:


I KNOW this is probably very simple, but I am scratching my head trying to get it to work...

I have a table with a key named approver_id

What I want to do is associate this to the User class

So I can do Event.approver.first_name, etc...

in Event I have

has_one :approver, class: 'User'

So how do I define the relationship to associate Event.approver_id to User.id

John

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bfbf73d2-2cf4-4c52-9896-5b58028f6f3d%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/56A0D151-B517-466C-BEAA-C4EC0C7E6BCD%40bichinger.de.
Reply | Threaded
Open this post in threaded view
|

Re: Association to User using a key named approver_id

John Sanderbeck
Sorry...  I was using class_name:   Typed it incorrectly here...

This worked for me...

  has_one :approver, class_name: "User", foreign_key: 'id', primary_key: 'approver_id'

John


On Sunday, January 26, 2020 at 1:24:18 PM UTC-5, Niklas Bichinger wrote:
Use „class_name“, not „class“ as option key. Also, from the Model that *holds the ID* of the other Model, it’s always „belongs_to“.

So in Event:
belongs_to :approver, class_name: „User“
and you’re good to go.


Sent via iPhone.

Am 26.01.2020 um 16:56 schrieb John Sanderbeck <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="Ngr6v6_qEQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">band...@...>:


I KNOW this is probably very simple, but I am scratching my head trying to get it to work...

I have a table with a key named approver_id

What I want to do is associate this to the User class

So I can do Event.approver.first_name, etc...

in Event I have

has_one :approver, class: 'User'

So how do I define the relationship to associate Event.approver_id to User.id

John

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="Ngr6v6_qEQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/bfbf73d2-2cf4-4c52-9896-5b58028f6f3d%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/bfbf73d2-2cf4-4c52-9896-5b58028f6f3d%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/bfbf73d2-2cf4-4c52-9896-5b58028f6f3d%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/bfbf73d2-2cf4-4c52-9896-5b58028f6f3d%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/19718c01-2b17-434b-90d4-866626ad7911%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Association to User using a key named approver_id

Niklas Bichinger
This should be much more easily done with belongs_to really...

has_one is used if the other model holds the id to this model. Which, if I understood correctly, isn’t the case in your code.

Am 26.01.2020 um 23:12 schrieb John Sanderbeck <[hidden email]>:


Sorry...  I was using class_name:   Typed it incorrectly here...

This worked for me...

  has_one :approver, class_name: "User", foreign_key: 'id', primary_key: 'approver_id'

John


On Sunday, January 26, 2020 at 1:24:18 PM UTC-5, Niklas Bichinger wrote:
Use „class_name“, not „class“ as option key. Also, from the Model that *holds the ID* of the other Model, it’s always „belongs_to“.

So in Event:
belongs_to :approver, class_name: „User“
and you’re good to go.


Sent via iPhone.

Am 26.01.2020 um 16:56 schrieb John Sanderbeck <[hidden email]>:


I KNOW this is probably very simple, but I am scratching my head trying to get it to work...

I have a table with a key named approver_id

What I want to do is associate this to the User class

So I can do Event.approver.first_name, etc...

in Event I have

has_one :approver, class: 'User'

So how do I define the relationship to associate Event.approver_id to User.id

John

--
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 rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bfbf73d2-2cf4-4c52-9896-5b58028f6f3d%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/19718c01-2b17-434b-90d4-866626ad7911%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/F5A65BDE-99E8-4175-83DE-878BFD3F140D%40bichinger.de.