Skip to content

Commit

Permalink
Fix has_many association on Rails 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
David Librera authored and stefanoverna committed May 28, 2015
1 parent 8f00d50 commit 789880f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/scoped_associations/activerecord4/has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def creation_attributes
def association_scope
super.where(reflection.foreign_scope => reflection.name.to_s)
end

def target_scope
super.where(reflection.foreign_scope => reflection.name.to_s)
end
end
end
end
Expand Down
45 changes: 44 additions & 1 deletion spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
describe 'ScopedAssociations' do
let(:post) { Post.new }


context "non polymorphic" do

describe "has_one" do
it "works with direct assignment" do
post.secondary_comment = Comment.new(comment: "foo")
Expand Down Expand Up @@ -70,6 +72,27 @@
expect(post.secondary_comments.size).to eq 0
end
end

describe "using more relation" do
before do
post.primary_comment = Comment.new(comment: "foo")
post.primary_comments.build(comment: "bar")
post.save!
post.reload
end

it "has one element" do
expect(post.primary_comment).to be_present
end

it "has many has 1 elements" do
expect(post.primary_comments.size).to eq(1)
end

it "not contain the has_one element inside has_many elements" do
expect(post.primary_comments).to_not include(post.primary_comment)
end
end
end

context "polymorphic" do
Expand Down Expand Up @@ -139,6 +162,26 @@
expect(post.secondary_tags.size).to eq 0
end
end

describe "using more relation" do
before do
post.primary_tag = Tag.new(name: "foo")
post.primary_tags.build(name: "bar")
post.save!
post.reload
end

it "has one element" do
expect(post.primary_tag).to be_present
end

it "has many has 1 elements" do
expect(post.primary_tags.size).to eq(1)
end

it "not contain the has_one element inside has_many elements" do
expect(post.primary_tags).to_not include(post.primary_tag)
end
end
end
end

0 comments on commit 789880f

Please sign in to comment.