Add belongs_to as DSL warning wrapper to has_one to help beginners#710
Conversation
|
FWIW, I lost 4+ hours trying to figure out how to wire up an ActiveRecord model that only had |
|
@olleolleolle @dgeb and I have been discussing this PR. On one hand we see the value of the alias, especially as evidenced by @smargh's report of wasted time. On the other hand JSON API only supports "has one" relationships with no distinction between has_one and belongs_to. We are trying to keep the As a compromise we'd like to see this implemented as a method, instead of an alias, and a deprecation type warning emitted informing the user that a simple has_one is more appropriate. If someone still wants to use belongs_to and doesn't want the message they can override the method on their own base resource class. What do you say? |
|
I like that plan! Let's write that message! Here's a first iteration
|
Prepare
|
@olleolleolle The message seems good to me. It's long, but that's probably a plus. Maybe we should output the particular resource class the message is coming for so the user can easily find the source. |
|
@lgebhardt There, edited using the GitHub pen. |
|
@olleolleolle Thanks! This should help cut down on confusion. |
|
I think the deprecation warning is the perfect solution. Once I figured out the conceptual gap between AR models and JR resources (JR doesn't care about which table the field lives in, only that it can call the association method on the model), I saw how and why |
|
IF we want to reinforce this concept more in the deprecation message, a wiki link, or a README link, could help the user get to that epiphany quicker. |
In order to reduce cognitive threshold for new users of JSONAPI::Resource, this PR introduces a method which does the same thing as
has_one.The user can write
belongs_to :thingas well ashas_one :thingand it means the same thing.These are equivalent:
The user is also warned with a deprecation warning that tries to teach them about why
belongs_tois the wrong name.