Need a strategy for an efficient autocomplete in Rails across multiple attributes - ruby-on-rails-3

I have a form for submitting an order, and I need an autocomplete field that searches across three attributes in the associated customer model: first name, last name, and customer_number (as opposed to customer.id). I know about the rails3-jquery-autocomplete gem found here http://github.com/crowdint/rails3-jquery-autocomplete, and got it working well, but a question has occurred to me -- is there a more efficient way to make the autocomplete work without having to query the db every time?
The other solution that occurred to me is to create a new indexed attribute in the customer model -- call it autocomplete_data. Whenever a new customer is added via the usual new customer form, an :after_create callback could populate the field. Would this speed up the performance? Or am I overthinking it?
UPDATE
I'm embarassed to say that I just didn't search hard enough the first time around -- I think this actually answers my question:
Rails: Efficiently searching by both firstname and surname

Related

Odoo 15 Find tax_ids Inside the account.move.line (Invoice) Model

Good day, hope everything's well.
I'm trying to find the value of tax_ids (Many2many field) inside the account.move.line model but i can't seems to find anything. I already access the psql of the database but i cant find tax_ids too.
I accessed that account.move.line model with ORM like this :
def _post(self, soft=True):
for move in self:
....
account_move_line = self.env['account.move.line'].search([('id', '=', int(move.id))])
print(account_move_line.tax_ids) #this find nothing
could someone please elaborate how is it possible to access id of the tax that applied to, in this case, an invoice line record?
Edit : Sometimes this ORM fetching the ID and sometimes it doesn't. But most likely it's not fetching.
I'm trying to find the value of tax_ids (Many2many field) inside the
account.move.line model but i can't seems to find anything. I already
access the psql of the database but i cant find tax_ids too.
tax_ids in account.move.line model is a Many2Many field, which is stored separately as another table in the database. This kind of relation field mostly be named something like this (main_table)_(related_table)_rel (ignore the parentheses). For example, this particular case's table is account_move_line_account_tax_rel since its main table is account_move_line and the related table for that specific field is account_tax. Inside the relation table, you will almost always find 2 fields mapping both ids together. For this case, it is going to be account_move_line_id and account_tax_id.
I accessed that account.move.line model with ORM like this :
def _post(self, soft=True):
for move in self:
....
account_move_line = self.env['account.move.line'].search([('id', '=', int(move.id))])
print(account_move_line.tax_ids) #this find nothing could someone please elaborate how is it possible to access id of the tax
that applied to, in this case, an invoice line record?
Edit : Sometimes this ORM fetching the ID and sometimes it doesn't.
But most likely it's not fetching.
Accessing a field via ORM always works as you intended if it is implemented correctly. In your code, you are searching account_move_line by id but you are trying to search it with move.id, which I assume it is account_move since you got the data sometimes. If you can access the lines that you want correctly, you will see that account_move_line.tax_ids will always give you the correct tax_ids. From what your code looks, I think you are trying to search the lines by its account_move. Therefore, your domain should be [('move_id', '=', int(move.id))] instead.

SRSS: Dynamic amount of subreports in a report

it might be possible I'm searching for the wrong keywords, but so far I couldn't find anything useful.
My problem is quite simple: At the moment I get a list of individual Ids through a report parameter, I pass them to a procedure and show the results.
The new request is like this: Instead of showing the list for all individuals at once, there should be a list for each individual id.
Since I'm quite a beginner in srss, I thought the easiest approach would be the best: Create a subreport, copy the shown list, and create a subreport per individual id.
The amount of this IDs is dynamic, so I have to create a dynamic amount of subreports.
Funny enought, this doesnt seem to be possible. This http://forums.asp.net/t/1397645.aspx url doesnt show exactly the problem, but it shows the limit of the subreports.
I even ran trough the whole msdn pages starting http://technet.microsoft.com/en-us/library/dd220581.aspx but I couldnt find anything there.
So is there a possibility, to create a loop like:
For each Individual ID in Individual IDs, create a subreport and pass ONE ID to this?
Or is there another approach I should use to make this work?
I tried to create a 'Fake'-Dataset with no sql query but just for iterating the id list, but it seems the dataset needs a data-source...
As usual, thanks so far for all answers!
Matthias Müller
Or is there another approach I should use to make this work?
You didn't provide much detail about what sort of information needs to be included in the subreport, but assuming it's a small amount of data (say, showing a personnel record), and not a huge amount (such as a persons sales for the last year), a List might be the way to go.
I tried to create a 'Fake'-Dataset with no sql query but just for iterating the id list, but it seems the dataset needs a data-source...
All datasets require a data source, though if you're merely hard-coding some fake return data, any data source will do, even a local SQL instance with nothing in it.

Selecting specific joined record from findAll() with a hasMany() include

(I tried posting this to the CFWheels Google Group (twice), but for some reason my message never appears. Is that list moderated?)
Here's my problem: I'm working on a social networking app in CF on Wheels, not too dissimilar from the one we're all familiar with in Chris Peters's awesome tutorials. In mine, though, I'm required to display the most recent status message in the user directory. I've got a User model with hasMany("statuses") and a Status model with belongsTo("user"). So here's the code I started with:
users = model("user").findAll(include="userprofile, statuses");
This of course returns one record for every status message in the statuses table. Massive overkill. So next I try:
users = model("user").findAll(include="userprofile, statuses", group="users.id");
Getting closer, but now we're getting the first status record for each user (the lowest status.id), when I want to select for the most recent status. I think in straight SQL I would use a subquery to reorder the statuses first, but that's not available to me in the Wheels ORM. So is there another clean way to achieve this, or will I have to drag a huge query result or object the statuses into my CFML and then filter them out while I loop?
You can grab the most recent status using a calculated property:
// models/User.cfc
function init() {
property(
name="mostRecentStatusMessage",
sql="SELECT message FROM statuses WHERE userid = users.id ORDER BY createdat DESC LIMIT 1,1"
);
}
Of course, the syntax of the SELECT statement will depend on your RDBMS, but that should get you started.
The downside is that you'll need to create a calculated property for each column that you need available in your query.
The other option is to create a method in your model and write custom SQL in <cfquery> tags. That way is perfectly valid as well.
I don't know your exact DB schema, but shouldn't your findAll() look more like something such as this:
statuses = model("status").findAll(include="userprofile(user)", where="userid = users.id");
That should get all statuses from a specific user...or is it that you need it for all users? I'm finding your question a little tricky to work out. What is it you're exactly trying to get returned?

Rails 3 Searching Multiple Models by created_at using sunspot

I'm trying to get a "What's new" section working in my Rails app that takes into account new records created for various tables that don't share any relationships. The one thing they do have in common is that they all have a created_at field, which I'm going to use to determine if they're indeed "new" and then I'm wanting to sort the results by that common field. I tried doing this with Sunspot, but I couldn't figure out how to make use of the the result set returned from the Sunspot search...
For instance in my Uploads and Article models I have:
searchable do
time :created_at
end
and in my search action I'll do this:
#updates = Sunspot.search(Upload,Article) do
with(:created_at).greater_than(1.hour.ago)
end
Which does seem to return something, if I do an #updates.total it returns the number of records I was expecting to find. Beyond this I'm not sure how to actually make use of the records. What I'd like to do is send #updates to a view and determine the model type of each record and then proceed to print out the relevant information, i.e names, descriptions, parent/child record information (for instance upload.user.username).
I might be going at this all wrong, perhaps there's a better option than sunspot for the simple search I'm attempting to perform?
Refer readme for details of how to use the search results. The method you are looking for is "results", which will give you first 30 results, by default:
#updates.results # array of first 30 results

What is the best way to fake a SQL array or list?

I'm building a chatroom application, and I want to keep track of which users are currently in the chatroom. However, I can't just store this array of users (or maybe a list would be better) in a field in one of my records in the Chatroom table.
Obviously one of the SQL data types is not an array, which leads me to this issue: what is the best way to fake/mock array functionality in a SQL database?
It seems there are 3 options:
1: Store the list/array of users as a string separated by commas, and just do some parsing when I want to get it back to an array
2: Since the max amount of users is allowed to be 10, just have 10 extra fields on each Chatroom record representing the users who are currently there
3: Have a new table Userchats, which has two fields, a reference to the chatroom, and a user name
I dunno, which is the best? I'm also open to other options. I'm also using Rails, which seems irrelevant here, but may be of interest.
Option 3 is the best. This is how you do it, in a relational schema. It is also the most flexible and future-proof option.
It can grow easier in width (extra columns say, a date joined, a channel status, a timestamp last talked) and length (extra rows when you decide there now can be 15 users in a room instead of 10).
The proper way to do this is to add an extra table representing an instance of a user being in a chatroom. In most cases, this is probably what you will want to do, since it gives you more flexibility in the types of queries you can do (for instance: list all chatrooms a particular user is in, find the average number of people in each chatroom, etc.) You would just need to add a new table - something like chat_room_users, with a chat_room_id, and a user_id.
If you're deadset on not adding an extra table, then Rails (or more specifically ActiveRecord), does have some functionality to store data structures like arrays in a SQL column. Just set up your column as a string or text type in a Rails migration, and add:
serialize :users
You can then use this column as a normal Ruby array / object, and ActiveRecord will automatically serialize / deserialize this object as you work with it. Keep in mind that's there are a lot of tradeoffs with this approach - you will never be able to query what users are in a particular room using SQL and will instead need to pull all data down to Ruby before working with it.