I Have a model "Msg" and it has a content as a string and a username as a string as well
at the moment all my Msgs have their content Lorem Ipsumed and I'd like to sort them out
I'm trying to do something like
Msg.all(:order => "content DESC")
but it will not sort the strings for me..
Is there anyway to sort the strings in one line to get all of the Msgs (or the actual strings)
Thanks
Im not sure to understand the question.
Msg.all(:order => "content DESC")
retrieves all messages ordered by its content. Consequently, mapping its content attributes returns all the strings properly sorted
Msg.all(:order => "content DESC").map(&:content)
Related
In my CloudSearch instance I want to give a greater priority to complete phrases instead of just word counts.
An example would be, when I search for "foo bar" I would like documents that have "foo" and "bar" next to each other be scored better than documents that have the two terms scatterred in the document. Of course, any other document containing either words should retrieved but not scored as highly.
Any ideas of how the query could be done ?
$searchQuery = array(
'query' => "(or ".$searchParam." (phrase boost=10 ".$searchParam."))",
'queryParser' => 'structured',
'queryOptions' => json_encode(array('defaultOperator' => 'or'))
);
This worked to some extend.
Totally random boosting value.
I have an array, "templates".
puts templates
gives me the following output:
{"id"=>4, "subject"=>"invoice", "body"=>"dear sirs", "description"=>"banking", "groups"=>"123", 0=>4, 1=>"invoice", 2=>"dear sirs", 3=>"banking", 4=>"123"}
I would like to "put" a certain element e.g. "dear sirs". I have tried:
puts templates[2]
but this just returns nil. What is the correct way to do this?
You access "dear sirs" using the key that's associated with it, "body":
puts templates["body"]
Suppose if you have hash like this
#a = {"id"=>4, "subject"=>"invoice", "body"=>"dear sirs", "description"=>"banking", "groups"=>"123", 0=>4, 1=>"invoice", 2=>"dear sirs", 3=>"banking", 4=>"123"}
And if you want to get value of key name 'body', then you can get output like this,
puts #a['body'] //Output = dear sirs
puts #a['subject'] //Output = invoice
For more information for ruby hash Ruby Hash
If you want to get a hash value by numeric index then you can do templates.values[index]
e.g
templates.values[0] => 4
templates.values[1] => "invoice"
templates.values[2] => "dear sirs"
Note: My answer is based on strong assumptions that may not be true. I have provided steps to validate that.
In-case you are on older ruby version you need to do puts templates.inspect in-order to print a Hash. Therefore suggesting your variable templates is a String. Best way to verify:
templates.class
#=> returns Hash or String accordingly.
If it return String, you can proceed as follows:
Convert the String into Hash
hash = eval(templates)
#=> {"subject"=>"invoice", 0=>4, "description"=>"banking", 1=>"invoice", 2=>"dear sirs", "id"=>4, 3=>"banking", "body"=>"dear sirs", 4=>"123", "groups"=>"123"}
Now that its a Hash you can access any value using its key like:
hash[key]
#=> val
Example for your case:
hash[2]
#=> "dear sirs"
If I create an XML element in HAML:
%tag{:b => "b", :a => "a"}
I get this output:
<tag a="a" b="b"/>
Is it possible to keep the ordering of the attributes in HAML?
I need this for a client-side code to display values in certain order and don't want to pass extra information on the client just to maintain ordering.
It's not possible with HAML because it's a hash.
In rails 3, I would like to do the following:
SomeModel.where(:some_connection_id => anArrayOfIds).select("some_other_connection_id")
This works, but i get the following from the DB:
[{"some_other_connection_id":254},{"some_other_connection_id":315}]
Now, those id-s are the ones I need, but I am uncapable of making a query that only gives me the ids. I do not want to have to itterate over the resulst, only to get those numbers out. Are there any way for me to do this with something like :
SomeModel.where(:some_connection_id => anArrayOfIds).select("some_other_connection_id").values()
Or something of that nautre?
I have been trying with the ".select_values()" found at Git-hub, but it only returns "some_other_connection_id".
I am not an expert in rails, so this info might be helpful also:
The "SomeModel" is a connecting table, for a many-to-many relation in one of my other models. So, accually what I am trying to do is to, from the array of IDs, get all the entries from the other side of the connection. Basicly I have the source ids, and i want to get the data from the models with all the target ids. If there is a magic way of getting these without me having to do all the sql myself (with some help from active record) it would be really nice!
Thanks :)
Try pluck method
SomeModel.where(:some => condition).pluck("some_field")
it works like
SomeModel.where(:some => condition).select("some_field").map(&:some_field)
SomeModel.where(:some_connection_id => anArrayOfIds).select("some_other_connection_id").map &:some_other_connection_id
This is essentially a shorthand for:
results = SomeModel.where(:some_connection_id => anArrayOfIds).select("some_other_connection_id")
results.map {|row| row.some_other_connection_id}
Look at Array#map for details on map method.
Beware that there is no lazy loading here, as it iterates over the results, but it shouldn't be a problem, unless you want to add more constructs to you query or retrieve some associated objects(which should not be the case as you haven't got the ids for loading the associated objects).
I need to generate a random url to my Topic model(for example) as such:
http://localhost:3000/9ARb123
So how can I do this in rails?
Note: the random string must contain digits, small and capital letters.
Something like this perhaps
#config/routes.rb
match "/:random_id" => "topics#show", :constraints => {:random_id => /([a-zA-Z]|\d){3,6}/}
will match a random string of 3-6 random letters/numbers to the show method of your Topics controller. Make sure to declare other resources above this matcher, as something like "http://localhost:3000/pies" will route to Topics#show instead of Pies#index.
To generate a random url for your Topic you can go something like this:
#app/models/topic.rb
before_create :generate_random_id
def generate_random_id
#generates a random hex string of length 6
random_id = SecureRandom.hex(3)
end
Patricks answer should work - but it only covers routing incoming requests.
If you're still using the standard routes (eg topic_path) to create your links, it will still use the normal routes.
If you run rake routes, you should see the name of the route you created with with the random_id. (You may need to name it using :as => 'random_route')
If you call that instead of the standard topic_path you should get the route you are after