Alan Reid

Joined

55,860 Experience
467 Lessons Completed
8 Questions Solved

Activity

Posted in Pull data from another table in a lookup

Guys thank you all so much for the extended discussion on this topic. Its really useful, and nice again more learning for me :D.

I have started to use it a lot else where in the site so it could be useful to have some of the lookups like this. I can also use this elsewhere in the app for other lookups.

Chris's example would work really well for me as I am using a similar lookup for users who don't manage the brands, but only follow them. So i could in theory retrieve articles for both there :)

Posted in Pull data from another table in a lookup

James you have a article i could look at regarding this?

If you have a portfolio it would be handy to have links too ;) Great idea btw Chris.

your telling me!

I been added likes to my app, now i am adding follows using a variant of the code you showed for the likes. And now for some reason i can't get the JS bit to work haha you have to refresh the page for the follow to register.

Cheers buddy. haha you know when you spend so long looking at the code you mind just goes blank :o lol

I was wondering if there is any way I can simplify this code, Ideally, I don't want to be looking up brands that are associated with the user, i want to do it on hit.

#Gets the ID associated to the current user
brand_ids = current_user.brands.pluck(:id)

#Looks up the articles associated with this user.
@articles_count = Article.where(brand_id: brand_ids)

Thanks in advance

You could add a Boolean field to your devise user table something like intro_viewed

Once they have done viewing the intro you could ping their user ID via and ajax request to a completed action where you update that field?

Or stick it in a cookie ;) no need to touch the DB then

Posted in Pull data from another table in a lookup

See this is why I love this site. The community is fantastic!

Posted in Pull data from another table in a lookup

haha Cheers guys :) Not to worry Jacob, thanks for your help mate, you made it easier for Chris really ;)

Posted in Pull data from another table in a lookup

I can't seem to get that to work.

Basically @articles = Article.where(brand_id: brand_ids) returns a list of articles which have a brand_id.

What i want to do is use that brand_id to get the name of the brand from the brands table and return it as part of @articles at present i then need to look up the brand id within the article, using
<%= Brand.find(article.brand_id).name %>
and obviously as we get more articles this could have loads of DB calls which is not ideal haha :)

Posted in Pull data from another table in a lookup

Hi all,
I am looking up a list of articles, depending on the brand ID's - @articles = Article.where(brand_id: brand_ids) what i would like to know is there a way to retrieve the brand name in this look up too so i don't have to do it later.

Maybe so it returns the article details + the brand name rather than the brand ID

Many thanks

Posted in How do i save a field in a separate table?

Hi,
I am trying to make sure a user is allowed to edit a product, however I'm not sure how i can check the user_brands table.

I have this check for the brands...

# Confirms the correct user.
    def user_allowed
      #Check to see if the user is allowed to access this brand
      @brands = current_user.brands.find_by(id: params[:id])
      # If they are not redirect them to thier brands managment page.
      redirect_to(brands_path) if @brands.nil?
    end

How would i go about checking that a user is allowed to edit a brand? I have this which gets all products for a brand

@brand = Brand.find(params[:id])
@products = @brand.products.all

Posted in How do i save a field in a separate table?

Hi,
I am trying to make sure a user is allowed to edit a product, however I'm not sure how i can check the user_brands table.

I have this check for the brands...

# Confirms the correct user.
    def user_allowed
      #Check to see if the user is allowed to access this brand
      @brands = current_user.brands.find_by(id: params[:id])
      # If they are not redirect them to thier brands managment page.
      redirect_to(brands_path) if @brands.nil?
    end

How would i go about checking that a user is allowed to edit a brand? I have this which gets all products for a brand

@brand = Brand.find(params[:id])
@products = @brand.products.all

Posted in How do i save a field in a separate table?

hehehe :D <%= collection_select(:brand_id, 0, current_user.brands.all, :id, :name) %>

this is why i love Rails! that would take hours of coding in .NET!!

Posted in Has any installed macOS Sierra yet?

All installed and working fine, however still get the home-brew message about 10.12 :)

Posted in How do i save a field in a separate table?

Now i have that sorted, i need to find out how to get the names of the brands for that user so that i can populate a select dropdown haha

Posted in How do i save a field in a separate table?

sorted it :D
so its actually @brand = current_user.brands.create(brand_params) instead of new

Posted in How do i save a field in a separate table?

i get this...

(byebug) @brand.user_brands
#<ActiveRecord::Associations::CollectionProxy []>
(byebug)

Posted in How do i save a field in a separate table?

na its not saving in the user_brands table but is saving the brand lol

I have reloaded the server too using rails s

Posted in How do i save a field in a separate table?

I have this...

class User < ApplicationRecord
  has_many :user_brands
  has_many :brands, through: :user_brands
class UserBrand < ApplicationRecord
  belongs_to :brand
  belongs_to :user
class Brand < ApplicationRecord
  has_many :user_brands
  has_many :users
  has_many :products
  mount_uploader :logo, PictureUploader

My migration was like this...

class CreateUserBrands < ActiveRecord::Migration[5.0]
  def change
    create_table :user_brands do |t|
      t.references :user, foreign_key: true
      t.references :brand, foreign_key: true

      t.timestamps
    end
  end
end

My create method

def create
    @brand = current_user.brands.new(brand_params)
    if @brand.save
      flash[:success] = "Brand added!! :D"
      redirect_to root_url
    else
      render 'new'
    end
  end
Earn XP by completing lessons, posting on the forum, and answering forum questions