#<ActiveRecord::AssociationRelation Active Record Query Ruby on Rails - ruby-on-rails

Further Updated
my tags table
create_table "tags", force: true do |t|
t.string "category"
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
Updated
Turns out I am getting the tags but not the pictures where its tags equal this value
#pics
#<ActiveRecord::AssociationRelation [#<Tag id: 3, category: "Length", name: "stupid", created_at: "2014-09-12 17:18:44", updated_at: "2014-09-12 17:18:44">]>
the above is from better errors
I am going through tags that belong to a particular user in the view
<% user.tags.each do |tag| %>
<li><%= link_to tag.name, user_path(user.url_name, user, tag_name: tag.name) %></li>
<% end %>
From checking my server log I am getting the value in tag_name successfully passed to the user_path
server log
Processing by UsersController#show as HTML
Parameters: {"tag_name"=>"brown", "name"=>"Judy ", "id"=>"8"}
The tag name is brown for the example above.
here is my users controller show method that process that tag_name
def show
#user = User.find params[:id]
if params[:tag_name] #my attempt
tag = params[:tag_name] #my attempt
#user.pics.each do |pic| #mine
#pics = pic.tags.where(name: tag) #mine
end #mine
else
#pics = #user.pics.page(params[:page]).per(20) #original
end
if an user clicks on a tag then the list of pictures who only has that tag connected to it gets shown.
the show view looks something like this
<% #pics.each do |i| %>
<div class="two-col inline-element">
<div class="col-md-12 white-container no-padding">
<%= link_to image_tag(i.picture.re_sized.url, alt: i.title), pic_path(i) %>
<div class="tag1">
Tags:
<% i.tags.each do |tag| %>
<%= tag.name%>
<% end %>
</div>
I am currently getting undefined method picture for #<Tag:0x007f96e0829438> which means #pic has nothing
This in rails console did come up with something.
User.last.pics.each do |pic| pic.tags.where(name: "stupid") end
=> [#<Pic id: 6, picture: "c19862b844b58ecbce39_110785.png", user_id: 8, title: "hh", caption: "hh", created_at: "2014-09-12 22:13:17", updated_at: "2014-09-12 22:13:17", hidden: nil>, #<Pic id: 5, picture: "d9c1b59d76ea44f0d1c7_110787.jpg", user_id: 8, title: "TestPic2", caption: "TestPic2", created_at: "2014-09-12 21:30:03", updated_at: "2014-09-12 21:30:03", hidden: nil>, #<Pic id: 4, picture: "90798f6c29618066a239_111061.jpg", user_id: 8, title: "TestPic", caption: "TestPic", created_at: "2014-09-12 21:25:34", updated_at: "2014-09-12 21:25:34", hidden: nil>]
thanks for the help!

You're over thinking the whole thing, your show method could be simplified a lot
def show
#user = User.find(params[:id])
#pics = params[:tag_name].present? ?
#user.pics.joins(:tags).where(tags: {name: params[:tag_name]}) :
#user.pics
end #add your pagination

Related

How to show organized data from rails helper content tag

How to show organized data from rails helper content tag?
Like below is my helper method and I want to show all categories name grouped by a parent, and that is ul li if you can please see below method I think you will understand that code & what do I want. That method outputted the data but not with ul li
The helper method
def category
parent_categories = Category.select(:id, :name, :parent).group_by(&:parent)
parent_categories.each do |parent, childs|
content_tag(:div) do
content_tag(:h1, parent)
end +
content_tag(:ul) do
childs.each do |child|
content_tag(:li, child.name)
end
end
end
end
the output of <%= category %>
{"Technology"=>[#<Category id: 1, name: "Programming", parent: "Technology">, #<Category id: 3, name: "Ruby on Rails", parent: "Technology">, #<Category id: 9, name: "Full Time", parent: "Technology">, #<Category id: 14, name: "Business Opportunities", parent: "Technology">, #<Category id: 15, name: "Contract & Freelance", parent: "Technology">, #<Category id: 18, name: "Engineering", parent: "Technology">, #<Category id: 25, name: "IT", parent: "Technology">],
"Education"=>[#<Category id: 5, name: "Industry", parent: "Education">, #<Category id: 6, name: "Education", parent: "Education">, #<Category id: 7, name: "Education & Industry", parent: "Education">, #<Category id: 16, name: "Customer Service", parent: "Education">, #<Category id: 17, name: "Diversity Opportunities", parent: "Education">],
"Other"=>[#<Category id: 8, name: "Part Time", parent: "Other">, #<Category id: 12, name: "Admin & Clerical", parent: "Other">]}
the schema.rb
create_table "categories", force: :cascade do |t|
t.string "name"
t.string "parent"
end
That was my done works.
After the example is what do I want like
Technology (Parent)
Programming
Ruby on Rails
Ruby
ReactJS
Education (Parent)
Office
Teacher
Physics
Other (Parent)
Clinical
Helth
etc...
Please help out me to done this works.
Thanks
You are using ERB in your helper but it is not an html.erb file so you don't get the tags you are trying to create. Why not just use the hash you've produced and then I think what you are looking for is something like:
The helper method:
def category
Category.select(:id, :name, :parent).group_by(&:parent)
end
And in your view file (.html.erb) do something like:
<% category.each do |cat, list| %>
<div class="category">
<b> <%= cat %> </b>
<ul>
<% list.each do |item| %>
<li> <%= item.name %> </li>
<% end %>
</ul>
</div>
<br>
<% end %>
OK, you can do it the way you are proposing using the concat method according to the documentation:
def category
parent_categories = Category.select(:id, :name, :parent).group_by(&:parent)
parent_categories.each do |parent, childs|
concat content_tag(:div) do
concat content_tag(:h1, parent)
end
concat content_tag(:ul) do
childs.each do |child|
concat content_tag(:li, child.name)
end
end
end
end

Rails: My page is rendering text results with html layout

My index page is rendering text result after html result.
[#<User id: 1, username: "mihracefendi", name: "Mihraç", bdate: nil, grad: nil, school: nil, city: nil, about: "Mikkembel bir oyuncu ve aynı zamanda seyis. Kendis...", created_at: "2017-10-04 15:49:54", updated_at: "2017-10-05 19:08:49", email: "mihracbey#gmail.com", avatar: "lohusa.jpg", gender: nil, lastname: "Cerrahoğlu">, #<User id: 2, ...>, ...]
screen shot of index
Why is it happen and how i fix this problem?
Thanks
You probably have some code that looks something like:
<%= #users.each do |user| %>
...
<% end %>
Which, instead, should look like this:
<% #users.each do |user| %>
...
<% end %>

Create a new hash out of Active Record results

I need to extract some of the questions i have in my database based on current_user and present them to the Current User.
So i have all questions in database, but i need only those that the current_user does not exist on the relationship between users and questions.
My code so far goes like this
#all_questions = Hash.new
counter = 0
ill_questions = Question.all
ill_questions.each do |q|
if !q.users.include? current_user
#all_questions[counter] = q
counter += 1
end
end
puts "these are all unanswered questions #{#all_questions}"
when i print #all questions it gives this
{0=>#<Question id: 9, question_type: "multiple", description: "This is a question", expl
anation: "", category_id: 2, created_at: "2015-11-05 20:02:05", updated_at: "2016-02-11 19:23:02", link_name: "", link: "",
video_url: "", image: nil, image_position: "explanation", metric: "metric test", imperial: "testers", description_type: tr
ue, restriction: false>, 1=>#<Question id: 10, question_type: "single", description: "This is another question", explanatio
n: "test", category_id: 10, created_at: "2015-12-10 12:57:10", updated_at: "2016-01-12 23:36:25", link_name: "", link: "",
video_url: "", image: nil, image_position: "question", metric: nil, imperial: nil, description_type: true, restriction: tru
e>, 2=>#<Question id: 11, question_type: "single", description: "correct-wrong", explanation: "", category_id: 11, created_
at: "2016-01-29 19:53:48", updated_at: "2016-01-29 19:53:48", link_name: "", link: "", video_url: "", image: nil, image_pos
ition: "question", metric: nil, imperial: nil, description_type: true, restriction: true>, 3=>#<Question id: 12, question_t
ype: "single", description: "New question", explanation: "", category_id: 10, created_at: "2016-01-29 19:54:18", updated_at
: "2016-01-29 19:54:18", link_name: "", link: "", video_url: "", image: nil, image_position: "question", metric: nil, imper
ial: nil, description_type: true, restriction: true>}
in my view i have this
<% #all_questions.each do |q| %>
<p class="question-title"> <%= q.description %> </p>
<% end %>
getting error on <p class="question-title"> <%= q.description %> </p>
undefined method "description" for #<Array:0x007fe15ba18c88>
the relationship between User and Question
User has_many :questions, :through => :trackers
Question has_many :users,through: :trackers
Tracker model
Tracker(id: integer, user_id: integer, question_id: integer, answered: boolean, correct: boolean, created_at: datetime,
updated_at: datetime, category_id: integer)
What i would like to show to the current user is the questions that he did not touch, as in he does not have a relationship yet.
i assume i have something wrong in my new structure, as i see not it became an array??
Try with:
#all_questions = Question.where.not(id: current_user.question_ids)
And then use #all_questions in your view just like you wrote.
Change
<% #all_questions.each do |q| %>
<p class="question-title"> <%= q.description %> </p>
<% end %>
To:
<% #all_questions.each do |_, q| %>
<p class="question-title"> <%= q.description %> </p>
<% end %>
If you really don't need the key, a better solution will be:
<% #all_questions.each_value do |q| %>
<p class="question-title"> <%= q.description %> </p>
<% end %>
#all_questions is a Hash, when use each on it, it passing the key-value pair as block parameters, you need two parameters for them, if you only provide one parameter, it will be an array like [key, value].
In your controller:
#all_questions = Question.includes(:users).where("questions.id NOT IN (?)", current_user.questions.pluck(:id))
In views:
<% #all_questions.each do |q| %>
<p class="question-title"> <%= q.description %> </p>
<% end %>

Rails: Controller dumps data in view

I have a simple controller with def index having the following code:
#companies = Company.all
respond_to do |format|
format.html
end
But in the view I get these errors appended after all the companies are being displayed:
[#<Company id: 4, title: "Testing #1(1)", created_at: "2013-02-05 19:14:04", updated_at: "2013-02-05 19:14:04">, #<Company id: 7, title: "Testing #1 1", created_at: "2013-02-05 19:34:48", updated_at: "2013-02-05 19:34:48">]
Updated with the view code:
= #companies.each do |company|
%li
.box
.c
%h2
= link_to company.title, company
= #companies.each do |company|
The equals sign in Haml means "print the result of this expression." You're seeing the array of companies on the page because you asked for it.
Use a hyphen instead.
- #companies.each do |company|

In my Rails app, JSON formatted results are appearing in a HTML view response

In a Rails 3.1 app, I have a controller returning a set of objects (children) in an index view using this code:
def index
#children = Child.all
#base_class = "children-index"
#title = "Your Children"
respond_to do |format|
format.html # children/index.html.erb
format.json { render :json => #children }
end
end
The index.html.erb view is written like so:
<h1><%= title %></h1>
<ul>
<%= #children.each do |child| %>
<li><%= link_to child.fullname, child_path(child) %></li>
<% end %>
</ul>
For some reason, the JSON response is getting thrown into the HTML response and I cannot determine the reason. None of my other index views are having this issue and their code is very close to the same.
John Jake Smith Jr
Jane Ann Doe
[#<Child id: 1, firstname: "John", middlename: "Jake", lastname: "Smith", suffix: "Jr", gender: "Male", dob_actual: "2011-01-05", dob_expected: "2011-01-01", height: 30, weight: 40, created_at: "2011-10-28 21:32:54", updated_at: "2011-10-28 21:32:54">, #<Child id: 2, firstname: "Jane", middlename: "Ann", lastname: "Doe", suffix: "", gender: "Female", dob_actual: "2011-05-05", dob_expected: "2011-05-01", height: 30, weight: 12, created_at: "2011-11-07 18:08:54", updated_at: "2011-11-07 18:08:54">]
That's not JSON, that's inspect output. You're getting that because each returns #children and you're using <%= here:
<%= #children.each do |child| %>
You want just this:
<% #children.each do |child| %>
Did you forget to do #children.to_json in your controller?

Resources