Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support belongs_to custom method name #186

Merged
merged 1 commit into from
May 10, 2018
Merged

Support belongs_to custom method name #186

merged 1 commit into from
May 10, 2018

Conversation

c910335
Copy link
Contributor

@c910335 c910335 commented Apr 27, 2018

Example:

class Article < Granite::ORM::Base
  # ...
  # `#author` and `#author=` with `field author_id : Int64`
  belongs_to author : User
end

Or both this feature and custom id in #142

class Article < Granite::ORM::Base
  # ...
  # `#author` and `#author=` with `field author_id : String`
  belongs_to author : User,  author_id : String
end

This is very useful because article.author is much more readable than article.user, and an article may need to belong to more than one kind of user, for example, author and approver.

end

macro belongs_to(model_name, decl)
# ditto
macro belongs_to(model, decl)
Copy link
Member

@robacarp robacarp Apr 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like decl is a pattern that is already used in this file, but what does it stand for? It seems like it means foreign_key? Can we call it something more descriptive?

Copy link
Member

@robacarp robacarp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor comment, but I like this improvement and could make use of it immediately. Thanks!

@c910335
Copy link
Contributor Author

c910335 commented Apr 28, 2018

@robacarp
I've renamed decl to foreign_key.
Thus the more readable syntax has shown.

  belongs_to author : User, foreign_key: author_id : String

And I think this syntax should be recommended.

@drujensen drujensen merged commit f25c453 into amberframework:master May 10, 2018
@conradwt
Copy link

conradwt commented Jun 18, 2018

@robacarp Is it possible to use belongs_to author : User and have the foreign key inferred to be id within the users table? At this time, I'm getting the following error message:

$ amber db seed
Error in db/seeds.cr:1: while requiring "../config/application.cr"

require "../config/application.cr"
^

in config/application.cr:4: while requiring "../src/models/**"

require "../src/models/**"
^

in src/models/friendship.cr:6: macro didn't expand to a valid program, it expanded to:

================================================================================
--------------------------------------------------------------------------------
   1.     belongs_to friend : Person, friend : Person_id : Int64
   2.   
--------------------------------------------------------------------------------
Syntax error in expanded macro: belongs_to:1: unexpected token: :

    belongs_to friend : Person, friend : Person_id : Int64
                                                   ^

================================================================================

  belongs_to friend : Person
  ^~~~~~~~~~

02:10:49 Database   | (INFO) Seeded database

Can someone direct me to the actual documentation for its usage? Also, I guess part of the confusion is that there exists granite and granite_orm packages. Should one be using granite here?

@Blacksmoke16
Copy link
Contributor

@conradwt You should be using granite. granite_orm is the legacy version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants