Alan Reid


38,810 Experience
304 Lessons Completed
7 Questions Solved


Posted in Need a hand with associations

Thanks Ivan,
A user is a user, they then need an account (for a business) which other users can be assigned to. This is where a user can be a member of an account. I understand the confusion.

later i can add in subscriptions to the mix to expand the app more.

Posted in Need a hand with associations

hi Daniel,
yeah a user can be the owner and be assigned to the account.

the updated code seems to work, but im all for finding out better ways to get things done.

Posted in Need a hand with associations

Just a quick update, after a little light reading this evening i think this maybe correct. It seems to work ok. If anyone notices something thats wrong please let me know :) thanks.


class User < ApplicationRecord
  belongs_to :account


class Account < ApplicationRecord
  has_one :owner, class_name: :User, foreign_key: :owner_id
  has_many :users

Posted in Need a hand with associations

I have a users model, and an accounts model. Basically I would like to have...

  • An account has one owner
  • An account can have many users
  • Users can only be a member of one account.

Does the following look right?


class User < ApplicationRecord
  has_many :accounts, foreign_key: :owner_id


class Account < ApplicationRecord
  belongs_to :owner, class_name: :User, foreign_key: :owner_id
  has_many :users

Posted in Thoughts on Rails 6, is it ready for production?

Seems like a no brainer then haha. No harm in starting a new project and just updating once it goes to stable, I will let you know how I get on.

Posted in Thoughts on Rails 6, is it ready for production?

Hey all,
So i thought i would pose the question and get other users views on Rails 6. So we are still on RC1, its been like this for a while now. I am curious to know others views, and experiences to help me work out if I should start building a new project in Rails 6 (some of the new features would be helpful).

I saw DHH said that its fine to use in production, and that Basecamp and other are using it without issue in its RC state.

Any way let me know

fingers crossed they dont mess too much, could be a decent browser. I wish Google would reduce the resources used though, still a beast of a browser.

yeah its really cool, i will look to use it on some other bits too i think. It will be a nice solution.

IE haha, I am counting the days til its axed :D I wonder how MS will mess up Chrome though haha

Yeah, I hated having it all in the form so start to mess around. I found the button_tag element could be attached to a form essentially so i can put the button anywhere on the page. It was just this edit issue i had which stumpped me haha.

So happy to get it all working now :D can finish this app off and move on to the next version :D

yeah I set the form using form: 'edit_product_' + params[:id] cheers as always mate :D

Yeah its outside the form tag.
You made me look at it again, and the form is using the ID of the item!

So doing the following fixes the issue!

<%= button_tag("Create", type: "submit", form: 'edit_product_3') %>

If you put the images in your app/assets/images directory, then you should be able to call the image directly with no prefix in the path. ie. image_url('logo.png')

I am using the following to submit my form when creating a product.

<%= button_tag("Create", type: "submit", form: 'new_product') %>

However, the issue I am facing is that when I try to do the same for editing a product, the form does not submit at all.

<%= button_tag("Create", type: "submit", form: 'edit_product') %>

My question is, what am I missing from this not to submit the edit form? I mean when I press the button, nothing happens at all. haha

I know there's bound to be simple answer thanks in advance

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 = I.product_id")
.from("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!

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2021 GoRails, LLC. All rights reserved.