Alan Reid

Joined

55,040 Experience
459 Lessons Completed
8 Questions Solved

Activity

Posted in Improve on these ActiveRecord requests?

I will give this a try, but i get the idea of having last 30 days as the default. It makes sense.

I could use JSONB to store the visits i suppose, and just update them every few hours.

Cheers Chris, given me something to think about this evening.

Posted in Improve on these ActiveRecord requests?

Do you mean have a column in the Products table which houses the count figure, and have a job to do the calculation each night, and update that figure?

That would be fine, but I may need to get the impressions for the last 7 days, 30 days, 6 months or even a year. So storing it may not be the mode reusable solution.

I have 2 tables, Products and Impressions which are related, and impressions belong to products.

maybe i need to get all products, and include the impressions count? that way it should be more efficient.

Posted in Improve on these ActiveRecord requests?

I am trying to get the top 5 products, based on the impressions that product has recieved.

Now the code below is working ok, but its not in the right order, and I cant work out a better way to get the impressions.

@products.joins("INNER JOIN products ON products.id = I.product_id")
.from(Impression.select("product_id, COUNT(product_id) as visits")
.group("product_id").limit(5).order("visits DESC"),:I)

Also, while i am here, is there also a more effiecient way to write out the getting of the total impressions for the last 30 days, based on a selection of products? So far i have the following...

Once again, it works ok, but it feels a little cumbersome. Maybe there is a i could combine both?

@impressions = Impression.joins(:product)
.where('impressions.created_at > ?', 30.days.ago)
.where(products: { id: @products.pluck(:id) })

As always so many ways to get stuff done. I will check out scopes later this evening.

Had to convert the parm to an int using .to_i but otherwise that worked well. However, as I had set the route "slug" to group_id. and I then had the issues that my nested routes were given the param group_group_id dammit! haha

Any way, I stuck the nested resources inside a member do and a little tweaking of the paths, everything seems to be working as expected. Maybe not the cleanest solution, but sometimes I do feel the strictness of Rails can be a negative.

Posted in RuntimeError (can't modify frozen String)

How about something like this?

add_breadcrumb "category / #{@title}", categories_path

Posted in New Site Design!

loving the new design and how its working. well done sir!

Posted in RuntimeError (can't modify frozen String)

What should add_breadcrumb 'category / ' << @title, categories_path output?

Posted in RuntimeError (can't modify frozen String)

Hi all,
I have user groups setup on my app using the code below...

class User < ActiveRecord::Base
  has_many :user_groups
  has_many :groups, through: :user_groups
end

class Group < ActiveRecord::Base
  has_many :user_groups
  has_many :users, through: :user_groups
end

class UserGroup < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

All is working as expected, users are assigned to a group, and can access the data for that group. I have come accross an issue where any user, can access ANY group. Obvioulsy this is not good haha.

Can anyone recommend how i would go about preventing access to a group if a user is not a member of the group they are trying to access.

I was thinking of using a before_action and using it there and other controllers when needed.

FYI, i also have products assigned to groups so have a URL structure (below) which measn the ID variable for the group changes, not sure if i could just make that stay as :group_id maybe?

/groups/{:id}
/groups/{:group_id/products
/groups/{:group_id/products/{:id}

Any help would be much appreciated. Thanks in advance.
Alan

Posted in Load only products based on region ideas please :)

Yeah this is what i was thinking. Cookies were my preffered way, it also means i can keep the url simple.

Thanks as always Jacob. Its good to get feedback on the ideas in my mind :)

Posted in Load only products based on region ideas please :)

Hi all,
Im trying to work out the best way to do a task.

On the users menu, they are able to select a region they are a member of. Once a user selects a region I want to load all data on the page based on that data until they select another, or reset it to all.

So if a user has not selected an option they will see all data for the regions they are a member of.

I have a couple of ideas, but is there a better way to do this?

Idea 1: have a :region_id in the URL - if none loads all. Other wise have a route like

  • site.com/region/overview
  • site.com/region/products
  • site.com/region/:region_id/overview
  • site.com/region/:region_id/products

Idea 2: Set a cookie, and load based on that?

Yeah, this is something I will have to look into in more detail.  I am new to the whole scraping of sites, but it's been a really cool learning experience and I am much more confident.

I did have a look at the import gem, and I am sure moving forward I can refactor the code better for scrapes.

This was an interesting post - https://blog.codeship.com/speeding-up-bulk-imports-in-rails/
It works perfectly. Just not a fan of the looping but it's a background task after all.

It will totally depend on the site, this one I am pulling in a global hit of 152, others may have less. Really enjoying Nokogiri and Sidekiq
Hey Jacob, 
Been a while!! :)

I have just been trying something similar, I did start to look at transactions. I think the main issue is that I am creating an array of items to insert. And as this is within sidekiq I am having to use plain ruby. 
Hi all,
I am creating new records, and want to be able to catch the ones that error and add them to my activity log so I can look into them and why they may have failed.

What would be the best way to do this?

Currently, I am doing the following by passing over an array of categories.

Category.create(categories)

I have validation on the name of the category to make sure its unique.

I would like to see which ones have failed and also have a total count of which ones were created vs failed. For example...

  • xyz failed to be created.
  • Created 99/100 categories, 1 failed.

I am using a simple activity log which I have made myself. And would like to have these details stored in the "log_msg"

ActivityLog.create(:act_type => "Insert", :act_action => "", :updated_by => "System", :activity => log_msg, :act_tstamp => Time.now)


Posted in Can anyone suggest me best Ruby on Rails 5 book?

I third The Ruby on Rails Tutorial, fantastic book! You can even follow it online at - https://www.railstutorial.org
But the book is also well worth buying for reference.

I also found Rails: Novice to Ninja quite good - https://www.sitepoint.com/premium/books/rails-novice-to-ninja

Also, check packt publishing and their free ebooks - https://www.packtpub.com/packt/offers/free-learning - they covers many different languages.

Posted in Update an attribute on belongs_to

Ok i seemed to have got it to work as i wanted, thanks for the guidance Chris :D

Posted in Update an attribute on belongs_to

I think the second option maybe quickest, for now, I can always go back and refactor the code to improve it later.  At present, I just need to be able to assign a `produduct_group` to a product and let it be updated when editing the product.

How would I go about doing this?

Posted in How do I generate dynamic classes?

I did something like this when I was working out the column widths once, maybe you could do similar.

$grid-columns : 16 !default;
$grid-gutter : 0% !default;
$column-width : 100 / $grid-columns;

// Lets calculate the column widths, outputs in %'s
@function grid-column-width($n) {
@return $column-width * $n - ($grid-gutter*($grid-columns - $n)/$grid-columns);
}

@for $n from 1 through $grid-columns {
%col-#{$n} {
width: grid-column-width($n);
}
}