HP

Joined

4,390 Experience
35 Lessons Completed
1 Question Solved

Activity

Posted in Ordering with included models

OK, I got it figured and posting here in case someone finds it useful.

I modified the Product model as follows

# product.rb
has_many :parts, -> { includes(:item).order(:part_numerical_value => :desc).order("items.name asc") }

The reason for doing this in the Product model is because I am listing multiple products on a page and using product.parts to get all matching parts for each product listed on the page (in the view).

The reason why the Item is singular in the includes part is because each part has a single item. Using includes(:items) results in an error when rendering the view. I use items in the ordering statement (i.e., "order("items.name asc") because it refers to the table name (SQL code).

I have tested this with multiple includes and it works fine. There is no particular order in which, ahem, ordering statements needs to be listed (i.e, in the above example, you don’t need to order first by a part column) so use whatever your app requires.

Posted in Ordering with included models

Is there a way to order results using a column from a model and a column from a related model via includes? Here is a scenario:

A product that has many parts, each of which has a numerical value and is linked to an item. What I would like to do when listing the parts is have them sorted/ordered first by their numerical value and then by the related item name. For example, if there are parts with numerical values (the letter indicates the related item name)

A : 10
B : 10
C : 50

they should be listed on the page as follows (numerical value DESC and the related item name ASC):

C 50
A 10
B 10


I am able to sort them by either numerical value or the related item name from the product model name but cannot do by both values as decried above using either models or controllers (the controller sorting defaults to created_at ASC).

All the solutions I found show how to order using multiple columns from a single model even when including a related model but not when ordering is done using columns from two related models. Because I am fetching multiple products per page, I get parts for each product using

product.parts.includes(:items)

Anyway, it is possible that I am overlooking something simple and that this has a straightforward solution so any pointers will be much appreciated.

Thanks in advance.

Posted in Row-level Multitenancy with ActsAsTenant Discussion

Late to the party and wondering if ActsAsTenant would work in the following scenario and how.

Basically, I have a company that has a many products, which are marketed to three verticals (consumer, commercial and industrial). All products are available to each vertical but have different a separate presentation for each (copy emphasizing appropriate features, custom photos, videos, brochure files, etc.).

The consumer vertical is default and is not available only on main domain (no subdomain for it). The commercial and industrial are only available on their subdomains (this will apply to any other specific verticals that will be added in the future).

In addition to this, there needs to be an admin area on a subdomain that would provide authorized users with access to all product presentations for all verticals.

The standard way I would approach this would require some code updates for each new vertical and I wondered if this could be better handled using ActsAsTenant given the scenario I outlined above (only the admin area/subdomain requires authentication, everything else would be publicly available).

Thanks in advance for any feedback.

Posted in Introduction to Importing from CSV Discussion

Late to the party and all that but if anybody is looking to seed their project database from a CSV file, the is what I enter in my seed.rb file(s):

'''
CSV.read(Rails.root.join('lib', 'seeds', 'file_with_data_to_import.csv'), headers: true).each do |row|
t = ImportingModelName.create(row.to_hash)
puts "#{t.name}, #{t.some_other_attribute} saved"
end

// Optional part
puts "There are now #{ImportingModelName.count} rows in your ImportingModelName database table."
'''

The same can be used in a Rake Task file as per Chris’s video. It assumes that the CSV file has header row but, for a scheduled task, you could take care of those on the fly as per the video.

Performance-wise, it works rather well. For example, I had to seed a project database with a bit fewer than 25,000 records, which took about 2 minutes on my development machine. Each record had 40 columns, 20 of which had to be validated, and I had to modify the text case of the content in 6 of the columns.

Hope this helps someone.

From my (fairly limited) experience, Hotwire may be easier to get started with but StimulusReflex is, at least at this point, more powerful and flexible, and perhaps more ambitious. Depending on what you plan on building, either could be sufficient/appropriate but you could combine them as well.

This allows you to have different functionality based on the device type a user access your app from. For example, you could use Hotwire/Turbo to show both a list of records and a detail on the same screen when the app is accessed/viewed from a desktop browser, and do more conventional list > detail individual page views on a mobile phone due to screen size limitations and, for example, inability to hover over links, etc.

The responsive CSS would still be in play for styling and what is shown and how, but you would also be able to use different style sheets altogether based on the device type any single user uses to access the app.

Posted in Contact form that sends email directly from the page

Hi Kyle,

Here are examples that may help you get the form working:

You can also share your code, if you feel comfortable, for additional help.

Based on the pre-6.0.2.2 version of Rails, I found Carrierwave to be more suitable for apps where I need to generate multiple-size versions of an image on upload that I may want to use outside the app itself (the ActiveStorage has not worked quite as well in a couple of apps of this type I built). Other than that, I find ActiveStorage to be rather nice, and it’s likely to get better in the future.

Have not had to do a migration from Carrierwave to ActiveStorage but, based on the available documentation, it looks doable but would require a lot of work so it may be better if one stuck to Carrierwave if that’s what they already do.

Knowing what kind of app you are building (in the most general terms) would help recommend one or the other.

Posted in How to use ActionText in Rails 6 Discussion

Not yet. The CSS approach would not have worked in this specific case so I treid mucking about in the source code but gave up as I didn’t feel like having to reivist the issue each time a Rails update rolls around.

I eventually settled on using Redactor as it gave me the control I needed for this project. Not as straightforward as ActionText though.

I do have some ideas on how to address the Trix editor configuration but have not had time to explore them. Will post here when I have something working.

I looked at both the ActionText and Trix documentation and could not find anything on how to disable/remove formatting tools and disable file uploads for ActionText.

For the project I am working on, I need only text formatting (bold, italic, lists, etc.) but not file uploads and hyperlinks. I considered dealing with hyperlinks via Rails content formatting options (rendering them as text) but was hoping for a bit more elegant approach before going down that route. As for blocking uploads, no idea whether that is possible while also using ActionText.

Has anybody deal with this alredy or does anybody know where I could look up more information?

Thanks in advance.

Posted in How to use ActionText in Rails 6 Discussion

Is there a way to limit the formatting options in the Trix editor? For example, if I don’t want users to enter links or upload images, is there a way I can configure the editor so that those options are disabled and not just not shown (I can do the latter via CSS).

Posted in How to use Action Mailbox in Rails 6 Discussion

How difficult, assuming it’s possible, would it be to adjust this for receiving replies via SMS (to use the episode’s example)?

logo Created with Sketch.

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

© 2022 GoRails, LLC. All rights reserved.