Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8906741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:43:59+00:00 2026-06-15T02:43:59+00:00

I have the following code in a likes_controller and it works fine as long

  • 0

I have the following code in a likes_controller and it works fine as long as a thing has an owner. If not, it breaks.

  def create
    @thing = Thing.find(params[:like][:liked_id])
    user = @thing.owner
    current_user.like!(@thing)
    current_user.follow!(user)
    respond_with @thing
  end

I’ve tried using

user = @thing.owner if @thing.owner.exists?

but I get a NoMethodError:

NoMethodError in LikesController#create

undefined method `exists?' for nil:NilClass

How do I check for the existence of an owner?

I also now notice I’ll have to put the second line (current_user.follow!(user)) in the block or it will break again…

Edit: This worked (using @Amadan’s answer):

def create
    @thing = Thing.find(params[:like][:liked_id])
    current_user.like!(@thing)
    user = @thing.owner
    if user
      current_user.follow!(user)
    end
    respond_with @thing
  end

Additional information: In case anyone ever actually uses this, I should point out that one more small change is necessary to get this to work. The above code errors out if a user tried to like a thing when they are already following the thing‘s owner.

so instead of

if user

I used

if user && !current_user.following?(user)

Hope this is helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:44:01+00:00Added an answer on June 15, 2026 at 2:44 am

    Insert this line:

    return unless user
    

    Some might not like it on stylistic grounds, so you can use this alternative:

    def create
      @thing = Thing.find(params[:like][:liked_id])
      user = @thing.owner
      if user
        current_user.like!(@thing)
        current_user.follow!(user)
        respond_with @thing
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which works perfectly fine, however I would like to
The following code works fine in chrome i have an observable property in the
I have the following code, which works as I expect. What I would like
My helper code looks like this (and works fine btw): module ProvidersHelper def call_to_review(provider)
Imagine you have two views with code like the following: controller_a/a.html.erb <%= content_tag(:div) do
I have the following code that seems like it should set my insertParameter but
I have code like following: switch(sort.Column) { case code: model = (sort.Direction == SortDirection.Ascending)
I have the following JavaScript code which I like to convert to jQuery but
I have the following code which I stripped out of any non-essential lines to
I have following line code in my view: <td> Model.some_instance_method(args) </td> I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.