Complicated where request in Rails - ruby-on-rails

I have two tables in my rails app: properties and requests. Both tables have field called address. But in property address is always single value like Hollywood for example and in request table there could be complicated string like ["Hollywood", "Beverley Hills"]. My task is to get all properties which match by address. It means that if we have in request ["Hollywood", "Beverley Hills"] i need all properties that have address as Hollywood and all Beverley Hills. I tried something like this:
#properties = Property.where("address = ? ", #request.address)
and:
#properties = Property.where("address IN (?) ", #request.address)
but both variants don't work and i think because #request.address is actually string, not array.
So i would like if somebody would suggest me some good solution.

Your first try is wrong, as address in Property is a single value.
Your second try is correct but not the best.
You can use Property.where(address: #request.address). But you have to be sure that #request.address is an Array of String.
You shouldn't save an Array as a String like that: "[\"Hollywood\", \"Beverley Hills\"]. It is too hard to parse in the application. If you want to save this way, you will be better using serialize :address, Array in the model, because then it will return an Array when you try to access the attribute.
Anyway, check if #request.address in an Array of String, if not, parse it to be an Array of String.

You can just wrap it in an array
#properties = Property.where(address: [#request.address])

Related

How to get active record's name after using find .where

Straight forward here:
<% #yyy = CityRace.where(city_race_id2: "3") %>
<% #xxx = #yyy.name %>
The #yyy is returning the proper record using the ID I have passed into it, but I'm trying to get the objects name. For some reason .name isn't working. Any idea what I'm going wrong here?
How do I find a record's name where id = a certain id?
where returns an ActiveRecord_Relationship, meaning an object containing CityRace objects for every row in the database with city_race_id2 equals to 3, no matter if there's only one, the result is an ActiveRecord_Relationship object, and that doesn't respond to name.
If you need the name of a particular object from that result, you can access to the specific element by its index and invoke name on it, e.g:
CityRace.where(city_race_id2: "3").first.name
Or to retrieve the name from every object:
CityRace.where(city_race_id2: "3").pluck(:name)
this one returns an array of strings, so, you must iterate over them to print them all or get a specific one.
In the other hand if you need a single row from the query, use find_by:
CityRace.find_by(city_race_id2: "3").name
.where returns an ActiveRecord::Relation which behaves like an array. You can think of it like a special kind of array that allows you to chain on more active record queries on it. When you call certain methods like each or to_a it evaluates the query into an actual array.
In any case, what you are looking for here is not something array-like. You want #yyy to refer to a single record.
Simple fix, just use find_by instead of where. Also take a look at https://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find and find vs find_by vs where

For each in by reference or value

I have the following code:
dim key
for each key in Request.Querystring
'do something
key = sanitized_param(key)
next
My question for you classic-asp connoisseur, does classic-asp, or asp in general, pass the variables as references(memory), or by value? Trying to figure out if I sanitize the key variable and pass it back to itself, is it just "alive" for that loop, or does the new value get passed to the original QueryString?
Request.QueryString retrieves the query string parameters by value from the page headers.
You can only make changes to a query string once its been retrieved via Request.QueryString, but you can't make changes directly to Request.QueryString as it's read-only (If you could make changes you would presumably use Response.QueryString, but this isn't a valid response command).
I'm guessing you're trying to sanitize all your query strings in one go? This isn't really possible or indeed necessary. You would typically sanitize a query string as and when you request it:
Response.Write(sanitized_param(Request.QueryString("myQS")))
Or to assign the query string to a variable first then sanitize it:
Dim myQS
myQS = Request.QueryString("myQS")
myQS = sanitized_param(myQS)
' or
myQS = sanitized_param(Request.QueryString("myQS"))
Once the query string has been assigned to a variable and sanitized you're able to reference that variable as often as you like without having to pass it to your sanitize function again.
Also, your example code doesn't make much sense. The key value in your for each loop is referencing just the names of your query strings and not their values. If Response.QueryString was a valid ASP command you would do:
Response.QueryString(key) = sanitized_param(Request.QueryString(key))
But again, this isn't possible.
EDIT: This solution might be what you're looking for:
Create a dictionary object, call it "QueryString" for example. Loop through all your query strings and add a sanitized version to the dictionary object.
Dim QueryString : Set QueryString = Server.CreateObject("Scripting.Dictionary")
For Each Item In Request.QueryString
QueryString.Add Item,sanitized_param(Request.QueryString(Item))
next
Now, to retrieve a sanitized version of a query string just use:
QueryString.Item("query_string_name")
Or for the original unsanitized version you could still use:
Request.QueryString("query_string_name")
Just like Request.QueryString, the dictionary object is forgiving and won't return an error if you ask for a query string that doesn't exist.
You could also create a function for retrieving sanitized query strings, for example:
Function SanitizedQS(ByVal qsName)
SanitizedQS = sanitized_param(Request.QueryString(qsName))
End Function
And rather than using Request.QueryString("query_string_name") just use SanitizedQS("query_string_name").

take an array of attributes to make a hash of only those attributes

I have an Item class with many attributes but for this case lets say it has a name, detail, inventory_count, price on it. in These are all ruby primitives.
I would like to get back a hash of like 'name' and 'detail' such that:
vals=%[name detail]
item=Item.find(23)
# somethign like???
g={}
vals.map { |atr| g[atr]=item.??? }
or some other technique. How would I take a string representation and get the attribute of an instance? Is there an easier / better way to do this?
edit 1
looks like send might do it. Let me check
#attributes returns an object in hash representation, #slice returns a hash containing only the given keys, so:
item.attributes.slice('name', 'detail')

Get default value from Erlang record definition?

Is there an easy way to get a default value from an Erlang record definition? Suppose I have something like this:
-record(specialfield, {
raw = <<"default">> :: string()
}).
I would like to have some way to retrieve the default value of the raw field. Something like this would be very simple:
#specialfield.raw % => <<"default">>
This is not possible. I would need to instantiate a record in order to get the default value:
Afield = #specialfield{}
DefaultValue = Afeild#specialfield.raw
DefaultValue % => <<"default">>
Is there an easier way of doing this? I seems like there should be some way to retrieve the default value without having to create an instance of the record.
How about:
raw_default() -> <<"default">>.
-record(specialfield, { raw = raw_default() }).
And now you have a function with the default in it. This will be extremely fast since it is a function call to a constant value. If this is also too slow, enable inlining.
Constructing an empty record and accessing one field can be done on one line:
(#specialfield{})#specialfield.raw.
Take a look at erlang - records, search section "11.8".
There's not much special about records - they're just a tuple at runtime. So to get the field raw from the tuple of default values that is the internal representation of #specialfield{} you would use:
element(#specialfield.raw, #specialfield{}).
In this case, #specialfield.raw is the index of the value for raw in the #specialfield tuple. When you pass in specialfield that resolves to a tuple in the form {specialfield, <<"default">>}.

Comma Delinated string, of Ids, from Array of Objects. RoR

Why doesn't this work?
I have an array of Objects, one of the attributes is the db id. I can make the array like so.
qc_parts.map!{|a| a.id}
However when I want to just make it a string. With
qc_parts.map!{|a| a.id}.join(",")
I only get an array out. I've also tried .to_s & .to_a
Any idea why this would be happening?
qc_parts.map!{|a| a.id}.join(",") will return a string, but it will not to put that value into the variable qc_parts. To do that you have to do
qc_parts = qc_parts.map{|a| a.id}.join(",")
If I've misunderstood, and you actually are seeing the join method return an array, then something strange is going on.

Resources