Reputation: 8759
I am using Angular in frontend and I am trying to send object such as:
obj = {
"foo" : "1",
"bar" : 2,
"baz" : 3
}
And when Rails recives this object, it should send back data from database depending on these parameters. This is my current method to create API:
def index
@tickets = Ticket.select(
'id',
'departure_date',
'departure_country',
'departure_country_tag'
)
render status:200, json: { tickets: @tickets}
end
However this fetches all data from database. I only want to fetch data that matches parameters from the object.
Any suggestion on how to do this properly?
Upvotes: 0
Views: 64
Reputation: 2927
Select allows you to specify the attributes to be returned. You need to add a clause to that query with the values from the front end.
@tickets = Ticket.select(
'id',
'departure_date',
'departure_country',
'departure_country_tag'
).where(foo: obj["foo"], bar: obj["bar"], baz: obj["baz"])
If you look in the console you'll see how the data is being passed and you can amend the active record query accordingly.
Upvotes: 1