Ruby on rails : some array information show up automatically - ruby-on-rails

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

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

How to group by day archives on rails blog?

I am new to rails, just finished Michael Hartl's tutorial.
I am creating a small blog as my first app, I am looking to sort all the homepage blog posts and group them by.
example: TODAY and all today's post...below that YESTERDAY and yesterday's posts, etc.
I see there is a method called group_by but can't figure out how to implement it
Can someone help?
See many people are looking for such a solution online.
Thanks,
Adam
There's an awesome gem for this groupdate, try it out, a sample code below
Blog.group_by_day(:created_at)
https://github.com/ankane/groupdate
You can use group_by
posts = posts.group_by{|post|
case post.created_at
when 0.days.ago..1.day.ago
"Today"
when 1.day.ago..2.days.ago
"Yesterday"
when 1.week.ago..2.weeks.ago
"Last week"
when 2.weeks.ago..3.weeks.ago
"2 weeks ago"
when 1.month.ago..2.months.ago
"Last month"
else
"Older"
end
}
EDIT
Above code will return you as Hash, in view
#posts = {"Older"=>[#<Contact id: 1, name: "sontya", email: "montya#mailinator.com", comments: "hi there", created_at: "2015-03-20
16:46:52", updated_at: "2015-03-20 16:46:52">, #<Contact id: 2, name: "faruk", email: "faruk#dispostable.com", comments: "hi there", created_at:
"2015-03-23 18:17:05", updated_at: "2015-03-23 18:17:05">], "Yesterday" => [#<Contact id: 1, name: "sontya", email:
"montya#mailinator.com", comments: "hi there", created_at: "2015-03-20 16:46:52", updated_at: "2015-03-20 16:46:52">, #<Contact id: 2, name:
"faruk", email: "faruk#dispostable.com", comments: "hi there", created_at: "2015-03-23 18:17:05", updated_at: "2015-03-23 18:17:05">]}
similar to this with your data, then
in view, loop through the Hash
<% #posts.each do |k,v| %>
<tr>
<td><%=k%></td>
</tr>
<% v.each do |post| %>
<tr>
<td><%=post.name%></td>
</tr>
<% end %>
<% end %>
I think using a database level group isn't correct, because it's usually used with sum or count or something like that, if you don't use any of those functions that applies on groups you'll end up with only one post for each group, which is in your case one post for each day, I think you should just select all and group them into arrays and loop in the view
posts = Post.some_home_page_query.order(:created_at)
#grouped_posts = posts.chunk{ |x| x.created_at.to_date }
# you can replace the block passed to chunk with whatever you
# see fit for your own case
In the view you could do something like this
- #grouped_posts.each |date, posts|
= "Posts for #{date.some_formatting}"
- posts.each do |post|
= render post # or whatever you want

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