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

polymorphic has_many no longer passes the relation.where(args) to first_or_initialize #11675

Closed
Nthalk opened this issue Jul 30, 2013 · 1 comment

Comments

@Nthalk
Copy link

Nthalk commented Jul 30, 2013

I've found an issue where model.has_many_polymorphic_models.where(condtion:value).first_or_initialize does not pass condition:value to the initialized polymorphic_model, whereas model.has_many_models.where(condtion:value).first_or_initialize passes condition:value to the initialized has_many_model.

The workaround is to use model.has_many_polymorphic_models.find_or_initialize_by(condition:value). But the issue of knowing where your polymorphic first_or_initialize calls are leads to confusing "Why is this nil?!" bugs.

pftg added a commit to jetthoughts/rails that referenced this issue Sep 28, 2013
In order to build associated records for owners which has not been saved
need to get where values to use as default attributes.
But for new record owner uses `ActiveRecord::NullRelation` which
override `where_values_hash` to return empty hash stub.

`where_values_hash` is not used to invoke any sql query, but good to
build others chains (even will be never executed) like:

```ruby
  post          = Post.new
  admin_comment = post.admin_comments.build

  assert_equal 'Admin', admin_comment.author
```

Closes rails#11376, rails#11676, rails#11675
@rafaelfranca
Copy link
Member

Closed by #11496

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