Thinking sphinx sorting on multiple fields - sorting

I am searching for the model Projects. Projects belongs_to Companies, so all Projects in the list can have the same Company attached to it. An example of a result list:
CompanyA - ProjectA
CompanyA - ProjectO
CompanyA - ProjectC
CompanyA - ProjectB
CompanyB - ProjectU
CompanyB - ProjectI
I can sort on Project name, but then the Company name column can be random like above. I would like to sort this column as secondary field. Can this be done? I have read that Sphinx is converting the fields sorting to an id, probably to save memory, but this can't possibly limit the sorting in this way?

You can use SPH_SORT_EXTENDED mode for this:
$sph->SetSortMode(SPH_SORT_EXTENDED, "Project ASC, Company ASC");
(sort mode docs)
Edit: above example is PHP but it appears that Thinking Sphinx exposes the feature similarly:
Article.search "term", :sort_mode => :extended,
:order => "Project ASC, Company ASC"

Related

Is there a way to retrieve objects of a %Library.RelationshipObject by a certain order

This documentation has no mention of sorting the elements
As an example I'll show sql query by class Sample.Employee in SAMPLES namespace, where properties Spouse and Company are relationships. You may get access to related object via -> and you can order by alias.
SELECT
title,
Spouse->Name,
Company->Name as CompanyName
FROM
sample.employee
ORDER BY
CompanyName

How to select from outside of a table's columns with a join in Activerecord?

I have the following two tables: user, project
User has many projects
Project belongs to a user
When I run the following
Project.find_by_sql("SELECT projects.*, users.is_admin
FROM projects
JOIN users ON users.id = projects.user_id
WHERE projects.id = 1")
The resulting object only includes fields from the projects table and users.is_admin is not included.
Note: I am aware of includes, I don't want to use includes because it is 2 queries instead of 1; In this particular case, it is very performance sensitive.
Note 2: I am aware of backing a model with a database view, I only want to do that as a last resort because it is another migration
Resolved!
Turns out it was not a problem!
It was not displayed but if you do project.is_admin the field is there.
For future references, you can do:
project.attributes to see!

Runtime join in lightswitch

I have a generic group members table with a GUID for a "group type" and a GUID for "referenced object". An example would be if I have a table of customers(each having a GUID) I can group them under "already paid" by creating a group GUID and in my "Group members table" referencing every customer by their respective GUID. This allows for any type of group to be added to the model as we expand(without adding extra tables).
Here is the problem. I have created a subquery in an entity in order to filter the universal group members table for a certain group and what "items" are and are not in that group; like so:
partial void ElementsNotMemberOfGroup_PreprocessQuery(int? UniversalGroupTypeIDParameter, int? UniversalGroupsIDParameter, ref IQueryable<UniversalGroupMember> query)
{
query = query.Where(x => x.UniversalGroup.UniversalGroupType.UniversalGroupTypeID == UniversalGroupTypeIDParameter);
query = query.Where(x => x.UniversalGroup.UniversalGroupsID != UniversalGroupsIDParameter);
}
This returns the GUIDs for the referenced object in the group, but for a user that's useless. I need to join this table and my customers table at runtime on the GUID so I can extract the customer info and display it.
Any Ideas?
LightSwitch wasn't really created with this kind of scenario in mind. LightSwitch makes things very easy for you when you create relationships between tables that are, well, "related". When you do this, you never need manual joins between entities.
While it's possible to do something similar to what you're describing (see the link below), it's a lot more work to achieve it, and in my opinion it isn't really worth the extra trouble. Not only that, but as you're discovering, it complicates even the most simple operations.
In essence, you're working against LightSwitch, instead of with it. My advice to you would be that if you really must do this type of manual optimization, then LightSwitch may not be the best product for you to use.
Beth Massi has a blog article, Using Different Edit Screens Based on Record Types (Table Inheritance), which isn't exactly what you're doing, but it may give you some ideas if you decide to still use LightSwitch for your project.

How to order an array by the name of a referenced table row, rather than it's ID

I apologize for any poor terminology I use, as I'm a pretty green (Rails) programmer. But here is my question: I'm working on a Rails app (2.3.8) that manages projects. Every project has one company (in a separate table) and one contact (in a separate table). I'm currently generating a list of projects which is ordered (1) by company and (2) by contact. The problem is that I need that list in alphabetical order (based on the name of the company/contact). As of now, the best I can do is order the list by the company/contact ID, which is what the projects table is storing. What are my options—if any?
Here is what my controller currently uses to order the list I need:
def jobs_to_be_invoiced
#to_be_invoiced_prep = Project.prepare_project("To Be Invoiced", "=")
#to_be_invoiced = #jobs_to_be_invoiced_prep.all(:order => 'company_id, contact_id')
end
You can try with, adding one line in your model for Company
default_scope order: 'companys.name ASC'
Let me know if it works for u.

I Don't Understand How to Express a Relationship Between Three Separate Tables

Given the following tables in ActiveRecord:
authors
sites
articles
I don't know how to express that an author is paid a different amount depending on the publication, but that authors working for the same publication have different rates:
John publishes an article in Foo for $300
John publishes an article in Bar for $350
John publishes an article in Baz for $400
Dick publishes an article in Foo for $250
Dick publishes an article in Bar for $400
etc.
What kind of relationship am I trying to describe?
At the moment I've got a "rates" table with author_id, site_id and amount columns. Given publication.id and author.id, I derive the cost of the article with
cost = Rate.find(:first, :conditions => ["author_id = ? and site_id = ?", author.id, site.id]).rate
That works, but I'm not sure it's the best way, and I'm not sure how to make sure I don't end up with 'John' having two rates for 'Baz.'
I don't think I want code so much as I want someone to say "Oh, that's a ... relationship" so I can get a grip on what I'm Googleing for.
Its a has and belongs to many with a rich join table.
class Author
has_many :publications, :through => :rates
end
class Publication
has_many :authors, :through => :rates
end
class Rate #rich join table
belongs_to :author
belongs_to :publication
end
And you can then simplify your finding like this:
#author.rates.find_by_site_id(123)
Plus you get direct access accross the join table
#author.publications
#publication.authors
Its straightforward, but I don't know if there's a specific name for this relationship.
It looks like you need three tables:
Author (info about authors)
Site (info about sites)
Rate Author/Site (rate info only)
In the third table you'd have at least:
Author ID (FK to Author, and Primary Key)
Site ID (FK to Site, and Primary Key)
Rate
And the rate table has two fields as primary keys with a unique constraint. And any joins involving the author and sites would involve a 3-table join.
When three entities are related to each other, it's called a ternary relationship.
Most of the relationships we deal with are binary, relating two entities to each other. For example, The "enrolled in" relationship between students and courses. Binary relationships are further categorized into many-to-many, many-to-one, and one-to-one. But you knew that.
Ternary relationships can be categorized as many-to-many-to-many, many-to-many-to-one,
many-to-one-to-one, and so on.
Binary and Ternary relationships can be further generalized to n-ary relationships.
Here's how I see the case you outlined: There are three entities: author, publication, and article. In addition to these three entities, there is a measure, namely rate. I could be wrong about this.
So I would see three entity tables:
Authors with PK AuthorID.
Publications with PK PublicationID.
Articles, with PK ArticleID.
Then there would be a relationship table with four columns:
AuthorID (FK),
PublicationID (FK),
AtricleID (FK),
Rate which is a currency amount.
The PK of this is (AuthorID, PublicationID, ArticleID)
Not that, in this design, there is no rate table. It's just a measure.
Note also that in this design, it's possible for several authors to collaborate on one article, and each be given a separate rate for his/her share of the article. That's not possible in some of the other proposed designs.
It's also possible for the same article to be sold to more than one publication. It might be desirable to impose constraints on the data, if the real world imposes the same constraints.
Anyway, if you want a search term to Google, the term is "ternary relationships".
I would use a third table:
author_site_mapper
------------------
id
author_id
site_id
rate
I've generally heard this referred to as a 'mapper' relationship. It signifies a many-to-many relationship between two tables.

Resources