How can I append an image to a model object in rails? For example, if I had a post, how could I upload/append an image to the object so that I can call it in the view dynamically? I know that the association would be a has_one relationship... Ive looked around and just can't find anything on this topic. Thanks.
The most common solution for model attachments in Rails is the Paperclip gem. Try using it, or take a look at a corresponding Ruby Toolbox category.
Related
I'm using the active_model_serializers gem for a RoR API. Versions:
Rails: 4.2.8
Ruby: 2.2.5
active_model_serializers: 0.10.0
I'm using a virtual attribute in a serializer. I get it by using a sub query when I retrieve the objects from the database.
You can find the code here: Github Gist
This is the error I'm getting:
undefined method 'number_of_reservations' for DiscountSchedule...
This field isn't defined in the table nor in the model (attr_accessor)
I'm not sure why it doesn't work, I have a very similar serializer and it's working OK.
Any help will be appreciated.
EDIT:
I have another serializer where the virtual/calculated field is working OK. My guess on this is that since AR is making a bunch of LEFT OUTER JOINS and the SELECT list of the query is very big at some point something is getting broke.
The link won't work for me as I don't have access at my work place, however, from the error I can recommend you to check if you have defined the attributes like this in your serializer attributes :number_of_reservations and have an action in the serializer that says
def number_of_reservations
// Your logic goes here.
end
I suspect this question has to be about ActiveRecord, rather than AMS. You're trying to use select and alias to collect some computed (aggregate) attribute along with objects themselves. This, unfortunately, won't work in ActiveRecord, at least not in versions below 4.2.X. And this is why you're observing this behavior, there is no number_of_reservations in your models.
To see what's going on, try to inspect #objects here: https://gist.github.com/LuisDeHaro/ebf92781b449aa1ee7b85f8f552dd672#file-some_controller-rb-L17
Indeed: the issue was by the big amount of LEFT JOINS that the includes(:table_name) is generating. The serializer then does not know what to do.
I found a monkey-patch gem that works for AR (Rails 4 & 5) that fix this.
https://github.com/alekseyl/rails_select_on_includes
So, the virtual field number_of_reservations is picked up by the serializer like a charm.
And, you might be wondering: why do you want to retrieve a field that is not in the table definition in the database. A: well, in some scenarios you will need a calculated field for EVERY row you are retrieving. A SQL sub query is one of the most efficient ways to do so.
It's working now for me.
I have a model Item that has an attribute :code.
Items are added to the database via CSV file uploads in rails. Each :item should have a product image associated with it.
The research I've done so far seems to suggest that bulk image uploads (think 500-1000 images) are best handled outside of rails.
My question is this: if I upload bulk images to S3, is there any way to associate images to their respective :item? For simplicity, let's assume that we can easily infer :code from each images filename.
The end goal is to display an items image with something like:
<%= image_tag("#{#item.image}") %>
Let me know if I can clarify, thank you!
You gave a little to few information (e.g. what gem do you use to store the images) for an optimal answer. So here is the answer for the given question: Add the following method into your Item model:
def image
"example_image_#{self.code}.jpg"
end
Please be aware that image_tag() will always result into an asset pipeline path. Please see http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-image_tag
I need to get all associated models from the other associated model, on which I want to run query first.
For example, I got Post model and Tag model. I need to get all Posts, which associated with some Tags.
There's no problem, if I have only one Tag – just call 'tag.posts', but if I have more, then one Tag – for example, I need to do somethink like:
Post.where(id: PostTag.where(tag_id: some_ids).pluck(:category_id).uniq)
I belive that Rails have a built-in solution. So, anybody knows it?
My thought is:
Post.joins(:post_tags).where('post_tags.tag_id' => some_ids).uniq
You can make it a scope for easier reuse. I don't think there is a built-in method for this situation.
In a specific view, I am rendering an array of POROs created from a SQL query (this is for performance reasons, having ActiveRecord models here would be a major performance problem).
In that view, I need to display an image uploaded with paperclip, but can't find a way of getting Paperclip to return an image url without an ActiveRecord (attachment) instance. I can get the name of the image from the DB ("image.jpg") and the Model name ("User") as well.
I would need something like url("image.jpg", :user, :thumb).
I could hardcode paths, but I would prefer not to, and let paperclip handle this.
Is this possible?
Thanks,
Ricardo
I created a gist for that here
Basically your object needs to implement some ActiveModel modules to fullfil Paperclip requirements, because this library is strongly coupled to ActiveRecord.
I am creating a blog in Rails using Scaffolding. I want to add a 'tags' field on each post like on StackOverflow and WordPress. I can do this with the string type ( rails generate scaffold post title:string body:text tags:string ) and then comma separated, but it's not good practice since I want the reader to browse by tags ( e.g. /tags/web20 or /tags/lol ). How can I do this?
Tagging is so common that implementations are a commodity. I believe "acts as taggable on" is usually the preferred way of implementing tags.
See other popular solutions here.
If you wish to implement it yourself, you could dive into the source code to find some ideas.
I would suggest creating a Tag model and using has_and_belongs_to_many to assign tags to posts. I don't know if the scaffold feature will help you create a form for that, but it shouldn't be difficult to add it yourself. I also suggest using the formtastic plugin as it's much easier and nicer to create forms with it.
Err, the usual way? Add Tag entity, add has_many :tags in your Post entity. Then migrate. That would be all.