Accessing data with joined table - mysql

How can I access this table with activerecord?
https://gyazo.com/1f37f5031c37967459c9bb18052886e9
Normally I just do Singularnameoftable.find(id).whateveriwant.
But for some reason this is not working.
<% #organisation_ids.each do |x| %>
<% #organisationjuridicalform << Organisation.find(x.to_i).juridicalform_id %>
<% end %>
<% #organisationjuridicalform.each do |y| %>
<% #aorgtype << Organisations_organisationtype.find(y.to_i) %>
<% end %>
This gives me following error :
uninitialized constant
ActionView::Base::CompiledTemplates::Organisations_organisationtype
I tried accessing the table using:
Organisation_organisationtype.find
Organisations_organisationtype.find
Organisation_organisationtypes.find
Organisations_organisationtypes.find
But all of them give the same error as if it doesn't know which table I mean.
Is there a special sytax for tables like this? I cannot seem to find anything about it when browsing online.
I need to acces this table in order to find the actual type in this table:
https://gyazo.com/1db9782b1f73e56a50ec660a3c83944c

Related

Ruby on Rails display activeRecord contents on HTML page

I am a ruby on rails beginner and am having some trouble displaying the contents of a database query on my html page. I have a list of compounds that I would like to print to the webpage.
In my controller I have the code:
#info = Info.where.not(compoundName: 'Empty').pluck(:compoundName)
And in my views page I have the code:`
<td><%= #info %></td>
which prints out the contents in the form: ["compound1","compound2",...]. Instead I would like to iterate over this and print each one on its own separate line. I have tried the code:
<% #info.each do |c| %>
<li><%= c %></li>
<%end %>
but this returns an invalid byte sequence error.
I then tried:
<% #info.each do |c| %>
<%= puts c %>
<%end %>
but that just prints to my terminal instead of the webpage. Can someone explain how to print out the contents of my query to an HTML page?
Thanks!
If your #info results are valid, this query works just fine:
<% #info.each do |c| %>
<li><%= c %></li>
<% end %>

rails activerecords query, row splitting

i have some records like this one
"boss/supervisor/employee"
"boss"
"boss/supervisor"
i would like to put all this records in a select input without repeating themselves... here is what i've done already
in my index i did this:
#jerarquy = Jerarquy.uniq.pluck(:name)
so when i get all the records from the column jerarquy:
<select>
<% #jerarquy.each do |jer| %>
<% jer.split('/').each do |j| %>
<option><%= j %></option>
<% end %>
<% end %>
</select>
i do get the data from the database but i think i have to change something in my controller in order to get the information i want in the order i want, for example if i select a boss i want to get /supervisor and /employee too but if i select a employee i want to only get a employee
This is how you would use flatten to convert the fields to a single array and then use .uniq
ary = ["boss/supervisor/employee", "boss", "boss/supervisor"]
ary.map { |string| string.split('/') }.flatten.uniq
=> ["boss", "supervisor", "employee"]
#jerarquy = Jerarquy.uniq.pluck(:name).map { |string| string.split('/') }.flatten.uniq
<select>
<% #jerarquy.each do |jer| %>
<option><%= j %></option>
<% end %>
</select>
However I would say that this code indicates a poor underlying design.
You should be letting the database handle pulling out unique records.

Displaying data from another two different tables on an index page

I'm struggling to get data from two separate tables to display on the same index page.
I have data from one table displaying as a list this table is called "transactions".
Now in my model for transactions I have defined that it has many "notes. which is my other table I want to display data from on this same index.
I want each transaction to display the notes associated with it by the id of the transaction and the id in the notes table under transaction_id.
For the life of me I have yet to figure out how to do this.
Here is what my transactions index.html.erb looks like:
<% #transactions.each do |transaction| %>
<div class="element">
<%= trash_ico(transaction) %>
<%= edit_ico(transaction, edit_transaction_path(transaction)) %>
<%= link_to "<h2><span class=\"el_header\">#{Asset.find(transaction.asset_id).nmc_name} : Assigned to #{User.find(transaction.user_id).name}</span></h2>
<div class=\"el\">Created: #{transaction.created_at}</div>
<div class=\"el\">Ended: #{transaction.finished}</div>
<div class=\"el\">Type: #{if ! transaction.transaction_type.nil? then transaction.transaction_type.name end}</div>
<div class=\"el\">Status: #{if ! transaction.transaction_status.nil? then transaction.transaction_status.name end}</div>
<br clear=\"all\" />".html_safe, transaction %>
<% note.each do |note| %>
<%= link_to "<div>#{note.notes}</div>".html_safe, note %>
<% end %>
</div>
<% end %>
And this is how the controller is defined for the index:
def index
unless params[:asset_id].nil?
#transactions = Transaction.find_all_by_asset_id(params[:asset_id])
else
#transactions = Transaction.all
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #transactions }
end
end
I'm very green to programming but mostly to rails. I see what the task is that I need to do, but I'm not sure what the syntax must be to do it.
I have a similar view setup for my show def. I just can't get notes to show underneath their respective transaction on the index.
I am thinking, something like this;
<% transaction.notes.each do |note| %>
<%= link_to "<div>#{note.notes}</div>".html_safe, note %>
<% end %>
Since you declared notes belonging to transactions. Rails allows you to access them this way.

Rails different headers for different pages with if statement, but headers persisting

I wrote a simple if statement to change the headers depending on which page the user is on like so:
<% if #miscpage = true %>
<%= render 'layouts/mischeader' %>
<% elsif #gallerypage = true %>
<%= render 'layouts/galleryhead' %>
<% elsif #photopage = true %>
<%= render 'layouts/photoheader' %>
<% end %>
Then in the controller for the specific pages the #variable is set to true. The problem though is that the mischeader persists whenever I go to any other page. It's like the application layout is not resetting the rendering process.
I've tested this, by switching the order of the if statements above, and indeed if #photopage=true were evaluated first, it's the photoheader that would persist across all pages.
I've further tested by writing separate if statements like below, and indeed, now I get two headers after visiting both the miscpage and the photopage.
<% if #miscpage = true %>
<%= render 'layouts/mischeader' %>
<% end %>
<% if #gallerypage = true %>
<%= render 'layouts/galleryhead' %>
<% end %>
<% if #photopage = true %>
<%= render 'layouts/photoheader' %>
<% end %>
Any idea how to fix this problem?
You really don't need to compare the variables against true boolean in an if statement. But for your case you are using assignment operator =, either change them to comparison operator == or get rid of the = true from all if and elsif statements.
x = true is an assignment. You are assigning true to x. It will always succeed. You want x == true to test equality.
= vs ==

Ruby on rails error in .each statement

<% temp1= "secondary" %>
<% #query = "select credentials_id from credentials where credentials_id LIKE '%" + temp1 + "%'" %>
<% Request.connection.execute(#query) %>
<% if #query.blank? %>
<p>These are no secondary users still created </p>
<% else %>
<% #query.each do |c| %>
<%= label_tag(:credentials_id,c.credentials_id)%>
<%end%>
<%end%>
I am trying to execute the code above, that I have written in my "admin.html.erb" file, but I´m getting the following error:
undefined method 'each' for #
The #query variable is just an String. If you want to iterate over every single character on that string, you should use each_char method.
I really believe that you're iterating over the wrong variable. I think you want to iterate over the result of your query, not over your query characters! For that you should do something like:
<% #query_result = Request.connection.execute(#query) %>
<% #query_result.each do |c| %>
<%= label_tag(:credentials_id,c.credentials_id)%>
<%end%>