rails find by path - ruby-on-rails-3

Maybe I'm not searching on the right keywords but: Is it possible to search given an object path?
I have '/businesses/2' and I'd simply like to do something like #object = Business.find('/businesses/2') to fetch that object
One way is to:
ids = params[:who_id].split('/')
#object = ids[1].singularize.constantize.find(ids[2])
But I'm wondering if there is a built in way since this seemed to me as something quite normal to do.

If you know that the id will be the last in the string then you can split the string by "/" and take the last element. If you're unsure, you may use regexp.
If you want also to perform a search depending on what's in your string (you do not know class name)) then use regexp to match a model name and then use these helpers:
http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html
to get a name of the class.
Like:
klass = _yor_extracted_string.singularize.constantize
object = klass.find(_id_here_)

Related

Jmeter use variable to create variable and retrieve value

I know the title may be confusing but this is what I need to do:
I have a list of variables I am pulling from Jquery extractor.
myVar_1 = 343
myVar_2 = 98763
myVar_3 = 5622
etc
I generate a random number between 1 and myVar_matchNr.
Then I want to navigate to a URL that has an ID of one of the randomly selected variables. For instance, this would be the path I would like as an example:
//mydomain.com/api/resource/${myVar_${randomNumber}}
Which would translate to ( in the case my random number was 2):
//mydomain.com/api/resource/98763
I have the random number, and I have the list of variables from the Jquery extractor, but I have been unsuccessful getting the value out of the combination.
I have tried the above URL directly.
I have tried a beanshell script that looked something like:
String myvarString = "myVar_" + get("myRandNumber");
String myVar = get(myvarString);
set("mySelectedVar", myVar);
That seemed to always come up an empty string.
Any suggestions on how to accomplish this?
You can combine 2 variables using __V() function like
${__V(myVar_${randomNumber})}
In regards to Beanshell, you need to use vars object which stands for JMeterVariables class instance to manipulate the JMeter Variables like
String myvarString = "myVar_" + vars.get("myRandNumber");
String myVar = vars.get(myvarString);
vars.put("mySelectedVar", myVar);
See Here’s What to Do to Combine Multiple JMeter Variables article for more information on the topic.
One more option is Random Controller, you can create 3 different request and place it under Random Controller.i hope this will serve the purpose.

Rails query the last of each type

I am using STI and have a table Widget that a bunch of other subclasses inherit from using STI. I want a query that gets the last created of each object with a uniq type.
I have a predefined array of types I want so lets say:
types = ["type1", "type2", "type3"]
So my query would be something like: (this doesnt work)
Widget.where(type: types).uniq.reverse
My goal is to get the last of each object that matches 1 of those types..
Not 100% sure, but something like this might work (untested):
ids = Thing.where(type: types).group(:type).maximum(:id).values
last_per_type = Thing.find(ids)
Thing.select("distinct type")
By the way, type is a special variable in rails and can't be used as a column name.

How do you get the field name, instead of the value, from the model?

Suppose I have a Model Class named Anything, in Yii, and all I want is to get not the field value, but the field name, how could I do that?
Because using something like:
$anything = new Anything;
$anything->field_name;
Returns the value of that field, which is the purpose for that, still, if all you want is the string of the name of the field, how could you do that?
I tried using:
$anything->attributes;
But it just returns an array of field names, I want to try and get a specific value as a defined constant.
What I want to do is use the $_POST with specific and practical use, so I wouldn't need to use:
$_POST["Model_name"];
Instead I could use:
$_POST[Anything::model()->name][Anything::model()->field_name->name]
Which seems a lot better than "" and '' here and there. Mostly because I'm trying to set multiple fieldsets of different Models in the same formulary.
So if I could use:
$_POST[Anything::model()->name][Anything::model()->field_name->name];
and
$_POST[Something::model()->name][Something::model()->field_name->name]
and
$_POST[Godspeed::model()->name][Godspeed::model()->field_name->name]
It would save a lot of problems I might had in the future.
$strModelName = 'ModelName'; //dynamic - whatever model name you put in it
$find_id = 3;
$record = $strModelName::model()->findByPK($find_id); //it's same with ModelName::model()->findByPK(3)
foreach($record->attributes as $key=>$value){
var_dump($_POST[$strModelName][$key]); //get value corresponding to given key
}
Btw, you still need check whether the model is exist or not
http://www.yiiframework.com/forum/index.php/topic/22790-check-if-model-exists/

Passing a block to .try()

This question is about using the .try() method in rails 3 (& a bit about best practice in testing for nil also)
If I have a db object that may or may not be nil, and it has a field which is eg a delimited string, separated by commas and an optional semi-colon, and I want to see if this string contains a particular sub-string, then I might do this:
user_page = UserPage.find(id)
if user_page != nil
result = user_page.pagelist.gsub('\;', ',').split(",").include?(sub)
end
So, eg user_page.pagelist == "item_a,item_b;item_c"
I want to re-write this using .try, but cannot find a way of applying the stuff after pagelist
So I have tried passing a block (from this link http://apidock.com/rails/Object/try)
user_page.try(:pagelist) {|t| t.gsub('\;', ',').split(",").include?(sub)}
However, the block is simply ignored and the returned result is simply pagelist
I have tried other combinations of trying to apply the gsub etc part, but to no avail
How can I apply other methods to the method being 'tried'?
(I dont even know how to phrase the question in an intelligible way!)
I think you're probably coming at this wrong - the quick solution should be to add a method to your model:
def formatted_pagelist
(pagelist || "").split(/[;,]/)
end
Then you can use it where ever:
user_page.formatted_pagelist.include? sub
Strictly speaking, I'd say this goes into a decorator. If you're not using a decorator pattern in your app (look into draper!), then a model method should work fine.

lua variable in pattern match

Im just wondering if it is possible to put a variable in a pattern match in Lua. Like something similar to the following:
var = "hello"
pattern = string.match(datasource, "(var)%s(a%+)")
The reason I need to do this is because the variable "var" will change periodically. (it will be in a loop)
Cheers in advance
Lua doesn't handle string interpolation inside of the quotes. Instead, you'll need to concatenate the parts with the var as a var reference and the rest as quote strings.
"("..var..")%s(a%+)" starts with a "(" as a string literal, concatenates the variable, then finishes off the rest of the string with a string literal.
Use "("..var..")%s(a%+)" instead.
I needed the same thing I think, a variable in a pattern match, but the above solution didn't work for me. I'm posting my solution in case it helps someone, didn't find anything else on the net like it.
I read a ': ' delimited file (name: tel) and want to search by name in the file and have the name and telephone number as answer.
local FileToSearch = h:read'*a' -- Read all the file
var = io.read() -- ask the name
string.gmatch(FileToSearch, ''..var..': '..'%d+') -- search for name, concatenate with number