How can I use rails includes method with a sunspot slor more like this query
Sunspot.more_like_this(xyz)
The xyz var is my Design model variable and i want to use include method to include associated Designer and Image model
I want to use the includes method outhere the way we use it for a sql query
Model.where(:something => something_else).includes(:model2)
Related
I am trying to use the asp-route- to build a query string that I am using to query the database.
I am confused on how the asp-route- works because I do not know how to specifically use the route that I created in my cshtml page. For example:
If I use the old school href parameter approach, I can then, inside my controller, use the specified Query to get the parameter and query my database, like this:
If I use this href:
#ulsin.custName
then, in the controller, I can use this:
HttpContext.Request.Query["ID"]
The above approach works and I am able to work with the parameter. However, if I use the htmlHelper approach, such as:
<a asp-controller="Report" asp-action="Client" asp-route-id="#ulsin.ID">#ulsin.custName</a>
How do I get that ID from the Controller? The href approach does not seem to work in this case.
According to the Anchor Tag Helper documentation, if the requested route parameter (id in your case) is not found in the route, it is added as a query parameter. Therefore, the following final link will be generated: /Report/Client?id=something. Notice that the query parameter is lowercase.
When you now try to access it in the controller as HttpContext.Request.Query["ID"], since HttpContext.Request.Query is a collection, indexing it would be case-sensitive, and so "ID" will not get you the query parameter "id". Instead of trying to resolve this manually, you can use a feature of the framework known as model binding, which will allow you to automatically and case-insensitively get the value of a query parameter.
Here is an example controller action that uses model binding to get the value of the query parameter id:
// When you add the id parameter, the framework's model binding feature will automatically populate it with the value of the query parameter 'id'.
// You can then use this parameter inside the method.
public IActionResult Client(int id)
I am using the Google Custom Search API to search for images. My implementation is using Java, and this is how I build my search string:
URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?"
+ "v=1.0&q=barack%20obama&userip=INSERT-USER-IP");
How would I modify the URL to limit search results, for example, to: 2014-08-15 and 2014-09-31?
You can specify a date range using the sort parameter. For your example, you would add this to your query string: sort=date:r:20140815:20140931.
This is documented at https://developers.google.com/custom-search/docs/structured_data#page_dates
Also if you use Google's Java API you can use the Query class and its setSort() method rather than building the URL by hand.
I think the better way is to put this into query itself. Query parameter contains 'after' flag which can be used like:
https://customsearch.googleapis.com/customsearch/v1?
key=<api_key>&
cx=<search_engine_id>&
q="<your_search_word> after:<YYYY-MM-DD>"
In my step-definition, I want to create a path to a named route for an arbitrary Model, like so:
Given(/^a new "(.*?)" form$/) do |model|
path = send("new_admin_#{model.downcase}_path".to_sym)
visit path
end
Used as
Given a new "Image" form
Given a new "User" form
I recall using a helper method in the past for this, one that allowed me to pass a modelname and some additional options, like the action and object or IDs. But I cannot find that anymore. This is for Rails 3.2.x.
Is there such a "magic" helper? If so, what is it? If not, are there common patterns to create paths to arbitrary Models?
Did someone try to integrate puret into rails_admin? I can't make a language switch to edit different translations :(
Changing I18n.locale forces whole rails_admin to use specified locale.
Now I got the solution. The two can work together well. In short:
Delete the pureted column(s) in your model
If you have the column pureted still in your model, rails form helper will bypass puret. That is, if a model called Post has a field called contents to be i18ned, the table posts SHOULD NOT have the column contents.
Actually we should use globalize3 instead. With this you do not need to remove the original column. And puret doens't support nested attributes assignment. globalize3 works very well.
I have a fairly large DB schema and about 100M rows with I would like to expose to the web, using Rails 3. By exposing to the web, I mean the following:
A REST api (json & xml)
Views to present the data hierarchically
Editors for specific parts of the data
Basically, what I am looking for is a way to run the rails scaffold command with the appropriate arguments automatically. I know that magic_model can do some parts of the reverse engineering itself, but it does not seem to be working with Rails 3.
Is there any tool that can automate the generation of scaffolding?
You could give the following gems a try:
ActiveAdmin
-> Though more of admin framework, it has an appeasing user interface and will aptly satisfy your scaffolding needs.
ActiveScaffold -> A simple auto-scaffold generation framework
I just changed a bit a script of mine:
#!/usr/bin/env ruby
require 'rubygems'
require 'active_record'
require 'active_support'
require 'logger'
require 'fileutils'
ActiveRecord::Base.establish_connection(YAML::load(File.open('database.yml')))
ActiveRecord::Base.logger = Logger.new(File.open('database.log', 'a'))
for table in ActiveRecord::Base.connection.tables
table_class=table.classify
eval("class #{table_class} < ActiveRecord::Base;set_table_name \"#{table}\";end")
columns = []
for column in Kernel.const_get(table_class).columns
columns << "#{column.name}:#{column.type}"
end
puts "rails generate scaffold #{table_class} #{columns.join(' ')}"
end
I tried it on a database of mine (I use only mysql) and I think its output is quite good. Basically it prints the scaffold commands. I don't know if it is a good solution for you but it's a fair starting point IMHO.
You can use reverse_scaffold. It does what the name implies, i.e. Automatically creates the scaffolding from existing table in the legacy database.
You can find it on github:
https://github.com/chrugail/reverse_scaffold (rails 3 version)
There is also a rails 2 version by ahe (the original author)