ActiveRecord has_many :through #order with Hash params #12730

Closed
firedev opened this Issue Nov 1, 2013 · 10 comments

Comments

Projects
None yet
5 participants
@firedev
Contributor

firedev commented Nov 1, 2013

I have a simple relation:

class Inquiry < ActiveRecord::Base
  has_many :attachments
  has_many :houses, through: :attachments 
end

And I wanted attachments to have some statuses to be able to select and sort houses based on them.

So when I want to have ordered houses displayed in a certain order I try this:

has_many :ordered_houses, ->{ order attachments: { id: :desc } }, through: :attachments, source: :house

That causes: ArgumentError: Direction should be :asc or :desc

->{ order 'attachments.id desc' } works, but it's not the way to go.

You might say that I have to write SQL manually in this case, yet using Hash works for where case: ->{ where attachments: { status: 'ok'} }

@robin850

This comment has been minimized.

Show comment Hide comment
@robin850

robin850 Nov 1, 2013

Member

Hello,

I think that it's a design problem. Could you try something like this please ?

has_many :ordered_houses, -> { order(id: :desc) }, through: :attachments, source: :houses
Member

robin850 commented Nov 1, 2013

Hello,

I think that it's a design problem. Could you try something like this please ?

has_many :ordered_houses, -> { order(id: :desc) }, through: :attachments, source: :houses
@robin850

This comment has been minimized.

Show comment Hide comment
@robin850

robin850 Nov 1, 2013

Member

Oops, sorry, I haven't put any explications. Actually, I think that there is two problems with your code:

  • I'm not sure whether order accepts nested hash like this
  • The source name should be plural I guess because the :through statement will actually call a specific method on the Attachement model here and it doesn't respond to house but to houses.
Member

robin850 commented Nov 1, 2013

Oops, sorry, I haven't put any explications. Actually, I think that there is two problems with your code:

  • I'm not sure whether order accepts nested hash like this
  • The source name should be plural I guess because the :through statement will actually call a specific method on the Attachement model here and it doesn't respond to house but to houses.
@firedev

This comment has been minimized.

Show comment Hide comment
@firedev

firedev Nov 1, 2013

Contributor

Hmm guess you're right, order is not that advanced. I kinda assumed it takes up the nested hash.

Regarding everything else: -> { order(id: desc)} produces ORDER BY "houses"."id" DESC. Attachment belongs_to :house so source: :house is correct.

Contributor

firedev commented Nov 1, 2013

Hmm guess you're right, order is not that advanced. I kinda assumed it takes up the nested hash.

Regarding everything else: -> { order(id: desc)} produces ORDER BY "houses"."id" DESC. Attachment belongs_to :house so source: :house is correct.

@robin850

This comment has been minimized.

Show comment Hide comment
@robin850

robin850 Nov 1, 2013

Member

@firedev : Ugh, actually in my example, a house belongs to an attachment and an attachment has many houses so yes, you are right source: :house should work. Can we close this then ?

Member

robin850 commented Nov 1, 2013

@firedev : Ugh, actually in my example, a house belongs to an attachment and an attachment has many houses so yes, you are right source: :house should work. Can we close this then ?

@melekes

This comment has been minimized.

Show comment Hide comment
@melekes

melekes Nov 2, 2013

Contributor

@firedev Just sent a pull request regarding this #12743

Contributor

melekes commented Nov 2, 2013

@firedev Just sent a pull request regarding this #12743

@firedev

This comment has been minimized.

Show comment Hide comment
@firedev

firedev Nov 4, 2013

Contributor

I would consider accepting the pull request.

Contributor

firedev commented Nov 4, 2013

I would consider accepting the pull request.

@firedev

This comment has been minimized.

Show comment Hide comment
@firedev

firedev Nov 8, 2013

Contributor

This should be reopened as this is a bug in Rails 4.0.1, it uses wrong tables in query:
#12743 (comment)

Contributor

firedev commented Nov 8, 2013

This should be reopened as this is a bug in Rails 4.0.1, it uses wrong tables in query:
#12743 (comment)

@rafaelfranca rafaelfranca reopened this Nov 11, 2013

@rafaelfranca

This comment has been minimized.

Show comment Hide comment
@rafaelfranca

rafaelfranca Nov 11, 2013

Member

Right.

Member

rafaelfranca commented Nov 11, 2013

Right.

@firedev firedev added the stale label Apr 23, 2014

@rafaelfranca

This comment has been minimized.

Show comment Hide comment
@rafaelfranca

rafaelfranca May 1, 2014

Member

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

Member

rafaelfranca commented May 1, 2014

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rails-bot

This comment has been minimized.

Show comment Hide comment
@rails-bot

rails-bot May 27, 2014

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rails-bot rails-bot closed this May 27, 2014

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