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

Adding an association object when parent doesn't exist fails #1

Closed
lundie opened this issue Dec 8, 2011 · 5 comments
Closed

Adding an association object when parent doesn't exist fails #1

lundie opened this issue Dec 8, 2011 · 5 comments
Assignees

Comments

@lundie
Copy link

lundie commented Dec 8, 2011

Hi,

I am having the following issue (using the spec example from your code).

class Account < ActiveRecord::Base
    has_many :projects
end

class Project < ActiveRecord::Base
    has_many :tasks
    acts_as_tenant :account
end

class Task < ActiveRecord::Base
    belongs_to :project
    acts_as_tenant :account
end

If I attempt to add a task without it belonging to a project the following SQL is run

t = Task.new(:name => "test")
t.save

SELECT COUNT(*) FROM "projects" WHERE "projects"."id" IS NULL 
    => false 

and the task is not saved. This is happening as well when I use accepts_nested_attributes_for within my model. I'm assuming rails is saving the Task before the Project and therefor creating this error.

Thanks for the gem and your help!

@ghost ghost assigned ErwinM Dec 8, 2011
@ErwinM
Copy link
Owner

ErwinM commented Dec 8, 2011

Hi,

I'm having some trouble recreating your error. I created a new branch in which I added a test that tests your exact usage case...and it passes.

Could I ask you to checkout the new branch I added (https://github.com/ErwinM/acts_as_tenant/tree/orphan_models) and make the test fail to illustrate your issue?

Thanks :)

@lundie
Copy link
Author

lundie commented Dec 8, 2011

Hi Erwin,

I believe it's failing without raising errors. If I run the following test it fails stating it returned false:

describe "It should be possible to create and save an AaT-enabled child without it having a parent" do
    @account = Account.create!(:name => 'baz')
    ActsAsTenant.current_tenant = @account
    Task.create(:name => 'bar').valid?.should == true
end

Thanks!

@ErwinM
Copy link
Owner

ErwinM commented Dec 8, 2011

Hi,

You are right of course: the model will fail its validation and not raise an error. Oversight on my part.

I think i added support for child models without a parent. I just released a new version (0.2.4) that contains the change and passes the spec above.

Could you let me know if this solves the issue in your code as well?

Thanks!

@lundie lundie closed this as completed Dec 9, 2011
@lundie
Copy link
Author

lundie commented Dec 9, 2011

That did it! Thanks for your help:)

@ErwinM
Copy link
Owner

ErwinM commented Dec 9, 2011

Glad I could help!

excid3 pushed a commit that referenced this issue Nov 17, 2020
Add 'counter_cache' to association options
excid3 added a commit that referenced this issue Nov 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants