Basic rails joins.. Result is a resource - ruby-on-rails

I'm quite new to rails and making an app that lists albums.
Each album should be loaded with two or three preview tracks from that album.
All I want to do is get the preview_url from the previews table where the album_id is the same as the current album.
In mysql, I would use a left join:
SELECT preview_id FROM previews WHERE album_id = '{$sanitized_album_id}'...
I'm just not sure how to achieve this in ruby.
I can't find a single example that isn't obsessed with posts and comments, and it just isn't clear enough to click yet.
My album controller method looks like this:
def show
#previews = Preview.where(album_id: #album.id).order(track_number: :asc)
end
And my show.html.erb file should loop the results with this (simplified) code:
<% #previews.each do |preview| %>
<iframe src="<%= preview %>"></iframe>
<% end %>
Although I see no errors and the right number of previews are loading for each album, the resulting code is nonsense to me.
I expect the preview variable inside the do loop to be something like
https://website.com/previews/1984628754234
Instead, it looks more like this:
#<Preview:0x00007fcc08572c38>
I guess this is the database resource or something, but not the actual data I [think I] requested.
All the youtubes and whatnot didn't help me and I've only got a few hairs left...
Any one of you fantastic coders able to spare a moment to show me my stupid mistakes?
Thank you!

This will load all the attributes of Preview, it internally is performing a SELECT *.
#previews = Preview.where(album_id: #album.id)
if looping through #previews you would need to call preview.preview_id on each entry to get the value for that column.
In order to get a query like you showed - SELECT preview_id - you could use Preview.where(album_id: #album.id).select(:preview_id). You would still need to call .preview_id on each entry in the list, but it wouldn't load additional data.
If you want to make a query but you just want the plain array of preview_id values, you can use Preview.where(album_id: #album.id).pluck(:preview_id).
See:
select
pluck

That's a Preview instance. You probably want a field on Preview like:
<iframe src="<%= preview.preview_url %>"></iframe>
You can't just load in the model.

Related

Raw and simple_form...neither is perfect

I have this in one of my models:
def rank_match_2
...
...
array = [start, finish, [words]]
results = field.insert(start, "<mark>") and field.insert(finish, "</mark>")
...
end
Now I know I shouldn't be adding view logic in the model, but I am building a search app that has quite a bit of logic built into how the results are rendered. At this point I don't see how to get around including it in the model, so I that's what I am doing.
My problem is this:
On the view, I have this:
<%=raw #parse.rank_match_2 %>
This effectively handles the html "mark" tags inserted in the model logic and DOES highlight the correct text, but DOES NOT include line breaks etc.
However, this:
<%= simple_form(#parse.rank_match_2).html_safe %>
does not handle the 'mark' tags and therefore DOES NOT highlight the correct text, but DOES correctly format the line breaks as expected.
I want to do both: highlight the correct text by inserting the 'mark' tags into the model object (which seems to work with 'raw'), AND render correctly formatted html with line breaks etc.
Any idea what I am missing. I am trying Draper gem but I don't think it is suitable for exactly what I want.

Load model resources angular.js

I have 2 tables posts and photos and there relationship is
post has_many :photos
photos belongs_to :posts
I load the posts through angular and display them to the user and all the content works perfect but when I try to display the photos they just don't show heres the code I am using
...loop through results
//this works
<h2>{{post.title}}</h2>
//this doesn't
<img src="{{post.photos.first.image}}" />
...end
I have been using Ruby on Rails and this is the syntax they use, I know its probably wrong but does anybody know the correct syntax.
Thanks
Try:
<img ng-src="{{post.photos[0].image}}" />
But check the model (the JSON return from Firebug, or debug from Firebug) to make sure the photos field exists and is array. The [0] should take the first item, if it exists. If it doesn't, you should better hide the image with ng-if.
Also make sure the path given by .image is correct for your application, you may need to enhance it as: ng-src="/correct/path/{{post.photos[0].image}}".

Ruby on Rails Loop Help Needed

I've been attempting to find a solution to this problem for about the last 12 hours and simply have to admit defeat and ask the question...
I am currently working on a project which involves added a start rating feature to a product.
the_number = number_with_precision(product.no_of_stars, :precision => 0) gives me the average star rating for the product. I can display the number (eg 4) on the index.html.erb page but cannot for the life of me get it to render a star jpg for for each number (instead of the number).
I am aware of the difference between <%= and <% etc however because this is my first post, I'm unable to copy and past the code I've used :-(
SO... the_number (for example) would give me 4, however the following loop construct within my index.html.erb file keeps throwing an error reading
undefined methodtimes for "4":ActiveSupport::SafeBuffer`
the_number.times do
img src="my-image.jpg" alt=""
end
I understand the snippet above is not wrapped in the correct tags, but as explained before, it will not allow me too. Please assume that the 1st and 3rd line are wrapped in ruby tags without the '=' and the image is wrapped in the relevant html img tag. You can also assume that the rating 'system' works correctly.
Any help would be greatly appreciated :-)
Thanks
the_number.to_i.times do
img src="my-image.jpg" alt=""
end
would <%= ... Integer(expression resulting in 4) %> work for you?
to this you can append times and continue with the loop
such as
Integer(the_number).times do
img src="my-image.jpg" alt=""
end
Try casting the_number to an integer:
the_number.to_i
Then you should be able to call times on it.

Adding forms in non-form pages in ActiveAdmin

I'm trying to add a simple select box and submit button to a "show" page in ActiveAdmin. Basically, the clients wants a simple way to assign a currently unassigned widget to the item currently being viewed. Not that that really matters.
What I am seeing is that although I can add a form and a select box, if I try to add anything after the select, the select doesn't get displayed. It's not that it is hidden by CSS, but that it just doesn't render.
Here's the relevant code:
column do
panel "Devices without locations" do
devices = Device.without_location
form_tag add_device_admin_location_path do
select_tag(:device_id, options_from_collection_for_select(devices, :id, :name))
submit_tag
end
end
end
The submit tag will be displayed, but the select will not. event if I put "foo" in there, only the "foo" will show up. The only time the select will show up is if there is nothing else in the block.
Update:
Okay, so I've been able to work around this by concatenating the output together. It's not ideal, and I definitely feel dirty, but it works.
I tried using formtastic on this, but it appears to only accepts attributes from the model, this doesn't work: I'm updating the device, not the location.
This works, but if anyone has a more better way of doing this, I'd love to know.
i had the same issue, and moving form into app/views/admin/#{model_name}s/_#{partial_name}.html.erb worked for me fine

How to set an initial value for my object in ruby on rails

I am still pretty new to Rails and am working on a basic app and have run into a problem, which I can't seem to get around, sorry if this question has already been answered, but searching didn't yield me any results.
Basically I am creating an app that catalogues all of someones's clothes, Now the main page is the index page of everything they have logged, and each picture is linked to the show page where more information is revealed. I want to use AJAX to bring that show page to the side of the main index page. I am working off of another tutorial to figure this out but have run into a problem. On the initial load of my index page, I need to define a variable for that show page so that it can be rendered using a partial, but I can't use find(params[:id]) since the user hasn't selected anything yet. So my question is how do I set that initial value to either just the first item in the catalogue or not have anything appear, and then when someone does click the link, the sidebar shows the more detailed description of the piece of clothing.
Thanks for any help.
#object = params[:id] ? MyModel.find(params[:id]) : MyModel.first
But I think there's some problem with design of application.
You might have some luck working with the ruby gem 'PJAX'. Here is a screen cast showing you how to get that AJAX sidebar you want. Good luck
It sounds like you can just print the container element as normal, but leave it empty when the page is generated. Optionally, hide it via CSS. Then, when you load its content with AJAX, set it to visible or just populate it as normal.
Alternatively, if you really want to set it to the first item in the catalog (or in any ActiveRecord) you can use .first e.g. Products.first, and use that value to populate its initial contents.

Resources