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

fixing bug with associations #574

Merged
merged 1 commit into from
Dec 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion spec/associations_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ describe Avram::Model do
describe "has_many" do
it "gets the related records" do
post = PostBox.create
post2 = PostBox.create
comment = CommentBox.new.post_id(post.id).create

post = Post::BaseQuery.new.find(post.id)

post.comments.to_a.should eq [comment]
post.comments.should eq [comment]
comment.post.should eq post
post2.comments.size.should eq 0
end

it "gets the related records for nilable association that exists" do
Expand Down Expand Up @@ -45,10 +47,12 @@ describe Avram::Model do
it "joins the two associations" do
tag = TagBox.create
post = PostBox.create
post2 = PostBox.create
TagBox.create
TaggingBox.new.tag_id(tag.id).post_id(post.id).create

post.tags.should eq [tag]
post2.tags.size.should eq 0
end

it "counts has_many through belongs_to associations" do
Expand Down
4 changes: 2 additions & 2 deletions src/avram/model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ abstract class Avram::Model
macro setup_association_queries(associations, *args, **named_args)
{% for assoc in associations %}
def {{ assoc[:assoc_name] }}_query
{% if assoc[:type] == :has_many %}
{% if assoc[:relationship_type] == :has_many %}
{{ assoc[:type] }}::BaseQuery.new.{{ assoc[:foreign_key].id }}(id)
{% elsif assoc[:type] == :belongs_to %}
{% elsif assoc[:relationship_type] == :belongs_to %}
{{ assoc[:type] }}::BaseQuery.new.id({{ assoc[:foreign_key].id }})
{% else %}
{{ assoc[:type] }}::BaseQuery.new
Expand Down