pull name attribute out of nested User array ruby - ruby-on-rails

board.voters_who_voted
returns an array containing rows from the User table. The attribute I want to pull out is the name.
Example output (only one user here):
[#<User id: 71, name: "montgomery Thamavaranacupt", email: "jmash#aol.com", created_at: "2013-01-27 05:32:30", updated_at: "2013-04-24 07:07:43", password_digest: "$2a$10$Es/olEq0.w6vcnn4.8v0.O0VXDP1c7nktcW85UFtG91e...", remember_token: "ttPm1aTE6WBm3WVx4EJTew", admin: false, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, avatar: 0>]
I suppose I need to iterate through the entire array and print the names.
This is what I tried:
board.voters_who_voted.each {|x| puts x.name}
however this just returns the entire array for some reason
board.voters_who_voted[0].name
the above properly returns the name of the first user but i need this for all of them

You can use map:
board.voters_who_voted.map(&:name)
Since you want to use this in the title attribute, I recommend the following:
<%= link_to .., .., :title => board.voters_who_voted.map(&:name).join("\n").html_safe %>

Related

Error in displaying data in Rails

I am trying to display data in the view for a member's profile. When I run the inspect option on the variable, it prints out all of the data on the profile variable. But, when I have it only call a column, I get an error.
I run the same code on a different variable and it prints out; so, I am a bit confused on what is going on. Is it because the Active Record has a relation? Here is the code:
profiles_controller.rb
def show
#show_page = params[:id]
#member = current_member
#profile = Profile.where(member_id: current_member.id)
end
show.html.erb
<hr>
<%= #show_page.inspect %>
<hr>
<%= #profile.inspect %>
<hr>
<%= #member.inspect %>
<hr>
<p>
<strong>Member ID:</strong>
<%= #member.id %>
</p>
View in Browser
"8"
#<ActiveRecord::Relation [#<Profile id: 6, f_name: "Test", l_name: "Member", u_name: "testing", security: "10", private: "1", avatar: nil, birthday: nil, phone: nil, address1: nil, address2: nil, city: nil, state: nil, zip: nil, long: nil, lat: nil, facebook: nil, twitter: nil, instagram: nil, pinterest: nil, googleplus: nil, motto: nil, created_at: "2017-12-23 05:15:53", updated_at: "2017-12-23 05:15:53", member_id: 8>]>
#<Member id: 8, email: "testing#t.com", created_at: "2017-12-19 20:02:34", updated_at: "2017-12-23 05:15:37">
Member ID: 8
Now, when I add the following code into the show page, I get an error.
show.html.erb
<p>
<strong>User Name:</strong>
<%= #profile.u_name %>
</p>
ERROR
Showing /Users/topher/Dropbox/railsapps/~sandboxes/temporary/app/views/profiles/show.html.erb where line #21 raised:
undefined method `u_name' for #<Profile::ActiveRecord_Relation:0x00007fcb2583b920>
Did you mean? name
I am just confused on if there is a different way that I need to be calling the data found in the variable. The only difference between the #member and #profile print outs that I can see is the #<ActiveRecord::Relation [ prefixed to #profile. Does this mean that I need to call the information differently?
#where is query method, and returns records matching query conditions wrapped in ActiveRecord::Relation object. Which explains why you are getting this error. To resolve it, you need to change it to:
#profile = Profile.where(member_id: current_member.id).first
Which will return first record matching given member id to #profile instead of ActiveRecord::Relation object.
However, you must use a finder method in case you want to find a particular record. So better and cleaner approach would be:
#profile = Profile.find_by(member_id: current_member.id)
Change the line in profiles_controller.rb like
#profile = Profile.find_by(member_id: current_member.id)
As you are using where clause on Profile it will return an array of ActiveRecord::Relation objects. But you need a single #profile object not #profiles object. Thats you should use find_by method instead of where clause.
I think the problem is your line on the show method
#show_page = params[:id]
You need to indicate model where to contain the params id
#show_page = Model.find(params[:id]) #=> model is your model which you can using
I think will help

How do I check to see if a model has a photo with Carrierwave?

I have a model - Post - that can have images & files uploaded to them. Two different uploaders in Carrierwave.
What I want to do is, do a simple check for the presence of a photo upload on an object and if it is detected I will display the image tag. But if it isn't, then it shows nothing.
I tried this:
<% if post.photo %>
<%= image_tag post.photo, size: "52", :class => 'entry-avatar' %>
<% end %>
But that doesn't work, it always shows this:
<img class="entry-avatar" height="52" src width="52">
When it isn't blank, it adds the file path, correctly, to the src attribute.
If I try to check for photo.nil? it doesn't work, even though it seems like it should:
> a
=> #<Post id: 1, title: "Fire at Bible College in Christian", photo: nil, body: "A massive fire is now raging at the Bible College ...", created_at: "2014-08-28 08:06:19", updated_at: "2014-09-18 23:35:04", user_id: 1, ancestry: nil, file: nil, status: nil, slug: "fire-at-bible-college-in-christian", publication_status: 1, has_eyewitness: true>
Notice that the :photo attribute above is set to nil...yet when I do this:
> a.photo
=> #<PhotoUploader:0x00000102e19878 #model=#<Post id: 1, title: "Fire at Bible College in Christian", photo: nil, body: "A massive fire is now raging at the Bible College ...", created_at: "2014-08-28 08:06:19", updated_at: "2014-09-18 23:35:04", user_id: 1, ancestry: nil, file: nil, status: nil, slug: "fire-at-bible-college-in-christian", publication_status: 1, has_eyewitness: true>, #mounted_as=:photo>
> a.photo.nil?
=> false
It doesn't work.
Is there a Carrierwave method that I can use to do this or some other way?
No need to check photo attribute with nil? method. You can directly check like a.photo? it returns Boolean value based on the whether file is present or not.

Ruby on rails : some array information show up automatically

I have a list on the webpage generated from database.
It first shows what I want : Beaf .
However after this information, some others information of the array show up automatically:
<Ingredient id: 1, name: "Beaf", groupid: 1, created_at: nil, updated_at: nil>
How can I remove it? Thank you very much!
# Instead of using
<%= #ingredients.each do |i| %>
# use
<% #ingredients.each do |i| %>
The = will output all ingredients, whereas your output should only occur in the loop itself

show data from table on Ruby

I am new on Ruby and I am trying get data from table. so when read this
<%= puts #note.inspect %> I have this this result.
[#<Note id: 1, user_id: 1, note_type: 0, text: "Barev dzez", lat: 40.2290542420142, lng: 44.420879046875, deleted: false, created_at: "2012-04-26 14:10:05", updated_at: "2012-04-26 14:10:05">]
So when I call Note.text (for instance) I got nil result. So what should I write here to get data from array?
Thanks
#note is an Array with one Note object. You need to get the element first. For example:
<%= #note.first.text %>
You are retrive record in an array so you need to call like this
<%= puts #note.first.text %>
or
<%= puts #note.last.text %> if there is only one record
But you don't specify how you are retrive records..

Ruby/Rails Way to Check if object in an Array exists in other Array

Think of these lines of code :
#boss_locations = BossLocation.order('min_level asc').all
#discovered = current_user.discovered_locations.includes(:boss_location).all
The first one gets all available boss locations. The second one, gets all the discovered user locations(user_id, boss_location_id) and also includes the boss_location object.
Now, in my view, i want to present every boss location and a message like 'Discovered' or 'Not Discovered', based on whether a boss location exists on #discovered.
Now, my question is how can i feed my view with an easy way to do that. I could just traverse both arrays, but i'm pretty sure it's not the better way. Maybe there is a nice way to map all the boss locations based on the discovered boss locations for the user. How would you do it ?
EDIT - The variables have :
#boss_locations :
=> [#<BossLocation id: 670261930, name: "Fire Swamp", location_index: 1, min_level: 5, needed_gold_to_open: 500, created_at: "2011-05-18 05:35:48", updated_at: "2011-05-18 05:35:48">, #<BossLocation id: 723149845, name: "Rabbit Remains", location_index: 3, min_level: 15, needed_gold_to_open: 3000, created_at: "2011-05-18 05:35:48", updated_at: "2011-05-18 05:35:48">, #<BossLocation id: 81327760, name: "Grateful Plains", location_index: 2, min_level: 10, needed_gold_to_open: 1200, created_at: "2011-05-18 05:35:48", updated_at: "2011-05-18 05:35:48">]
#discovered :
=> [#<DiscoveredLocation id: 736487645, user_id: 986759322, boss_location_id: 670261930, created_at: "2011-05-22 05:37:01", updated_at: "2011-05-22 05:37:01">, #<DiscoveredLocation id: 736487646, user_id: 986759322, boss_location_id: 723149845, created_at: "2011-05-22 05:37:06", updated_at: "2011-05-22 05:37:06">, #<DiscoveredLocation id: 736487647, user_id: 986759322, boss_location_id: 81327760, created_at: "2011-05-22 06:01:35", updated_at: "2011-05-22 06:01:35">]
This is a lot of logic to put in a controller or view; consider creating a discovered? method on the BossLocation model. That way you could iterate through #boss_locations and call discovered? on each:
<% #boss_locations.each do |bl| %>
<div>
<%= "#{bl.name}: #{bl.discovered?(current_user)}" %>
</div>
<% end %>
The method would probably look like this:
class BossLocation < ActiveRecord::Base
def discovered?(user)
user.discovered_locations.map(&:boss_location).include?(self)
end
end
I commented above and you seemed to like the idea, so I wrote it out.
To encapsulate your data-model better, you'll want to modify your model class.
class BossLocation < ActiveRecord::Base
has_many :discovered_locations
has_many :users, :through => :discovered_locations
def discovered_by?(user)
self.users.include?(user)
end
end
Then all you need in your controller is:
#boss_locations = BossLocation.order('min_level asc').all
And in your view:
<% #boss_locations.each do |boss_location| %>
<div>
<%= boss_location.name %>:
<%= boss_location.discovered_by?(current_user) ? 'Discovered' : 'Not Discovered' %>
</div>
<% end %>
As the objects in the two arrays are not identical, you might need to iterate in a custom way:
<% #boss_locations.each do |boss_location|
<div>
<%= boss_location.name %>:
<%= #discovered_boss_locations.inject('Not Discovered') { |result, element| result = 'Discovered' if element.boss_location_id == boss_location.id}
</div>
<% end %>

Resources