rails/rails — Issue #3757: join all selected for two tables change parent table id
Author: avastor
Created: 2011-11-26 11:06:11+00:00
State: open

# tickets.rb
class Ticket < ActiveRecord::Base
belongs_to :client
scope :find_for_client, ->(ticket_id, access_token) {
select('*').
joins(:client).
where('tickets.id' => ticket_id, 'clients.access_token' => access_token)
}
end

# client.rb
class Client < ActiveRecord::Base
has_many :tickets
end

# routes.rb
resources :tickets do
collection do
get ":id/read/(:access_token)", :to => "tickets#show", :as=> "clientread"
end
end

class TicketsController < ApplicationController
# GET http://localhost:3000/tickets/34/read/c38b8e2916a7d662d432bcc49966e0e434b23927
def show
@ticket = Ticket.find_for_client(params[:id], params[:access_token]).first
end
end

If I set select('*') in my scope
scope :find_for_client, ->(ticket_id, access_token) {
select('*').
...
}
then I got
Ticket id: 33, ... , client_id: 33.
or no set select('*') is good result Ticket id: 34.

Sorry for my bad English