show data from table on Ruby - ruby-on-rails

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..

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 to display value from this array in Ruby on Rails

I have been working on a side project and I'm kind of stuck here
So I extract some info in the form of an array using
<%= #link.pixels.select{|x| x['platform']=='Facebook'} %>
Where the result is
[#<Pixel id: 1, platform: "Facebook", name: "Facebook", pixel: "189874014884804", created_at: "2017-10-17 18:44:13", updated_at: "2017-10-17 18:44:13">]
How can I extract the value of pixel from this array?
Also, is this the correct approach to select? What if there are two duplicate entries in Pixel with the platform name "Facebook"?
I have been stuck at this for while now.
If what you are trying to do is display the value of the pixel attribute from a collection of Pixel instances where the platform matches the string "Facebook" this is what you'd do:
<% #links.pixels.each do |pixel| %>
<% if pixel.platform == 'Facebook' %>
Pixel: <%= pixel.pixel %>
<%end%>
<%end%>

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

postgres array and rails 4 erb

i have a view in my postgres database that returns an array on my frequencies column. unfortunately, it sometimes returns values like {NULL} (due to the raw data).
in my rails view, i have something like:
dataset = [
<% #item.each do |i| %>
{
name: "<%= i.device %>"
lng: <%= i.longitude %>
lat: <%= i.latitude %>
frequencies: <%= i.frequencies.to_s.html_safe %>
},
<% end %>
]
which appears to work great - except when it reaches a record that contains {NULL}:
in the javascript console it shows:
Uncaught ReferenceError: nil is not defined
and in the html it shows:
...
}, {
name: "blah",
lng: -122.2,
lat: 37.4,
frequencies: [nil]
}, {
...
i could fix this by iterating through the list in the controller, but i think this would be rather long winded (and a waste of cycles).
is there a way i can get the erb to output the 'correct' [] in (instead of [nil]) json when it's null?
Try this:
<%= i.frequencies.compact %>
compact example:
[nil].compact #=> []
[1,2, nil, 3, nil].compact #=> [1,2,3]

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