Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Checking if there's any record in my database


  • Please log in to reply
9 replies to this topic

#1 andrewliu

andrewliu

    Signalman

  • Members
  • 17 posts

Posted 15 August 2013 - 04:59 AM

I'm trying to check if there's any records in my database, right now my code in my views is this:

 

 

        <% if @user.styles.image? %>
          <div>
            More Photos
          </div>
        <% end %>
 
I'm getting error saying that 
 
undefined method `image' for []:ActiveRecord::Relation
 

Any suggestions?

 



#2 Norm

Norm

    Passenger

  • Members
  • 7 posts
  • LocationPrescott, AZ, USA

Posted 15 August 2013 - 05:26 AM

As far as I know the object? is only defined for a boolean.   I would use something like

<% if @user.styles && @user.styles.image %>

This assumes that @user.styles may be nil and so may image.

 

I do not know if this is optimal but it works


  • RubyOnRails4ROR likes this

#3 uokesita

uokesita

    Passenger

  • Members
  • 1 posts

Posted 15 August 2013 - 05:43 AM

I think is because Rails @user.styles doesn't return an array of styles,

it returns an ActiveRecord::Relation



#4 stephen

stephen

    Passenger

  • Members
  • 1 posts

Posted 15 August 2013 - 08:11 AM

If User has_many :styles, then you need to do something like this:
 
    <% if @user.styles.first.image? %>


#5 elgalesmana

elgalesmana

    Passenger

  • Members
  • 1 posts
  • LocationBandung

Posted 15 August 2013 - 08:33 AM

hi, you can try this :

 

<% if @user.size > 0 %>

  <div>

    More Photos
  </div>
<% end %>


#6 Jamie

Jamie

    Controller

  • Moderators
  • 114 posts
  • LocationThe UK

Posted 15 August 2013 - 10:37 AM

 

hi, you can try this :

 

<% if @user.size > 0 %>

  <div>

    More Photos
  </div>
<% end %>

 

 

I'm a fan of this method. But as said above it's due to the fact is a ActiveRelation which will be returned and not always have something.


Rails developer based in Newcastle, UK.
Web app owner - Twitter lover

#7 AstonJ

AstonJ

    Dispatcher

  • Members
  • 35 posts

Posted 15 August 2013 - 12:14 PM

Need to see more about your set-up tbh. Do images belong_to users? If so you could check for that, or you could add a counter cache/user.styles_mage_count field and then simply see if that is above 0


Edited by AstonJ, 15 August 2013 - 12:14 PM.

:)


#8 Andre Dublin

Andre Dublin

    Signalman

  • Members
  • 13 posts
  • LocationCharleston, SC

Posted 15 August 2013 - 01:36 PM

You should probably avoid check for records in the view.

 

It would be better to have a helper that does that so you end up with

<%= ImagesPresenter.has_images(user) %>

Your ImagesPresenter would then return the html of images that the user has.

On second thought I'd probably name this something like UserImages instead.

This is just scratching the surface of presenters, and its a trivial example there is so much more you could do to this example.



#9 andrewliu

andrewliu

    Signalman

  • Members
  • 17 posts

Posted 15 August 2013 - 04:16 PM

I figured it out. All I had to do was add .blank?

<%= @user.styles.image.blank? %>


#10 noz

noz

    Signalman

  • Members
  • 21 posts

Posted 15 August 2013 - 08:49 PM

Why is the @user variable an ActiveRecord::Relation? If you're only expecting a single user record to be returned you should probably use the find method, or at the very least, call on first/last to whatever finder method you're currently using so you're not returning an array. On the other hand, ensure you properly pluralize your variable names if you're expecting more than one result. 

 

 

hi, you can try this :

 

<% if @user.size > 0 %>

  <div>

    More Photos
  </div>
<% end %>

 

 

Leaving the variable name aside, I prefer to call the any? method in this scenario, it's less verbose, and reads a lot nicer. If you're only checking for the existance of records and not doing anything else, try the exists? method instead, it's faster than checking the size and reads well.


  • james likes this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users