Here's my Schema
class Menu < ActiveRecord::Base
belongs_to :menuable, :polymorphic => true
end
class Page < ActiveRecord::Base
has_one :menu, :as => :menuable
end
class Links < ActiveRecord::Base
has_one :menu, :as => :menuable
end
I want to link to a polymorphic class in the Menu view using link_to, e.g.
<%= link_to menu.name, menu.menuable %>
This works, but this retrieves the menuable object from the database, when all I wanted is to generate a link. You can imagine if my menu is large, this will really bog down my application.
When I decared the menuable field as polymorphic, Rails created menuable_type and menuable_id. What can I use to generate a link to the polymorphic page, short of writing a helper function with a giant switch statement (e.g. if I have a large number of menuable 'subclasses'?)
It's been long since the question was asked but I had the same problem recently and the solution was to use polymorphic_url. You need to find the name of the route you need to create a link to, for example "fast_car_path" and make it out of your *_type and *_id from polymorphic table. For example, you have a list of comments and want to make the link to the cars that they belong to. So if *_type = FastCar we have
#comments.each do |comment|
link_to polymorphic_url(comment.commentable_type.tableize.singularize, :id => comment.commentable_id)
which will generate "fast_car_path" without downloading the cars from database.
I am a noob in rails and I dont know how good that advice is, but I hope it will be helpful for somebody.
You could do something like this:
def my_menu_url(menu)
"/#{menu.menuable_type.tableize}/#{menu.menuable_id}"
end
if you use the rails convention for naming the controllers that correspondent to your models.
But don't do it. You work around the routing mechanism of rails and that's simply bad practice.
You should use the :include option in your finders to eager load your menuables:
Menu.all :include => :menuable
In the case this isn't enough you may use some sort of caching.
Another approach could be to use url_for[menu.menuable, menu]. So, the link tag would look like so: <%= link_to menu.name, url_for([menu.menuable, menu]) %>.
you could use polymorphic routes for this
https://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html
<%= link_to menu.name, polymorphic_path(menu.menuable) %>
it will generate html like this
menu.name
Related
I am trying to call value from another model inside the views.
tse.headoffice.head_office_id
Defined the relationship in headoffice.rb as
has_many :tse
and in tse.rb as
belongs_to :headoffice
Now I am getting an error as undefined method
undefined method `head_office_id' for nil:NilClass
<% if tse.headoffice.present? %>
<%= tse.headoffice.head_office_id %>
<% end %>
try() lets you call methods on an object without having to worry about the possibility of that object being nil and thus raising an exception
<%= tse.try(:headoffice).try(:head_office_id) %>
Assuming that the HeadOffice model has an attribute called head_office_id:
<%= tse.headoffice.head_office_id if tse.headoffice %>
If that's not the case:
<%= tse.headoffice_id %>
Something about this doesn't look right. Usually the has_many reference is plural. It's possible your naming scheme is messing with Rails' opinionated magic.
Also why would headoffice have a field called headoffice_id? Wouldn't it just have a field called id? Finally, one nitpick, it should be called head_office not headoffice. And tse is not a good name either. What is tse? Spell it out if you can and form it in a manner that can be singular or plural. Rails works much better if you follow these simple naming guidelines.
https://gist.github.com/iangreenleaf/b206d09c587e8fc6399e
See the simple example below:
post.rb
has_many :comments
comment.rb
belongs_to :post
To access a post's comments you'd type the following:
Post.first.comment.body
Or if you're uncertain about a post having a comment you'd say:
Post.first.try(:comment).try(:body)
my guides can have many guide_pics. In my show guide view, I want to show the pics. I'm trying to use
<%= image_tag #guide.guide_pics if #guide.guide_pics %>
Instead of the image, the page renders with the text:
So it seems like something is there, I just have to get the picture.
But in the debugger this gives
#<ActiveRecord::Associations::CollectionProxy []>
Does that mean an empty object is returned? If that is the case, maybe I seeded the db wrong:
g1.guide_pics.build(picture: File.open(File.join(Rails.root, '/public/2015-08-28 18.55.47.jpg')))
Otherwise, maybe I set up the association wrong.
My guide_pic model
class GuidePic < ActiveRecord::Base
belongs_to :guide
validates :guide_id, presence: true
validates :picture, presence: true
default_scope -> { order(created_at: :desc) }
end
My guide model
class Guide < ActiveRecord::Base
belongs_to :user
has_many :guide_pics, dependent: :destroy
#mount_uploader :picture, PictureUploader
end
guide_pics is an association, and will return one or more guide_pics.
So you will have to iterate over all guide_pics, as follows:
<% #guide.guide_pics.each do |guide_pic| %>
<%= image_tag guide_pic.picture.url %>
<% end %>
Notice I write guide_pic.picture.url: I am assuming you are using a gem for your attachments, like carrierwave or something similar, which can build a url for your image --if not, you will do add that yourself.
But if you just want to show the first picture, you could do something like
<%= image_tag #guide.guide_pics.first.picture.url if #guide.guide_pics.count > 0 %>
Rails is doing correctly, you are misunderstanding.
while a model is having many pictures (has_many relation) rails is auto-generating a method to access those.
In this case Guide.guide_pics is making a query to the DB, something like
SELECT * from guide_pics where guide_id=5
as you can see - this is selecting all rows, the hole set of data which is associated with one guide. This is what ActiveRecord is called an ActiveRecord::Collection.
First of all, yes, you seeded the DB wrong!. The .build method is not saving anything to the database, you should call the .create method.
If you are having any objects in your database you have 2 (3) ways of rendering the image.
<%= image_tag #guide.guide_pics.first.url if #guide.guide_pics.any? %>
This will take the first picture from the Collection if any is in there.
This is bad code.
another option would be to say .take instead of .first.
better code would be something like
<%= image_tag #guide.preview_picture %>
to do so you need a model function
class Guide
def preview_picture
guide_pics.first.url || "/images/no-logo.jpg"
end
end
this will automatically takes the first picture or returns the string of a default one.
my advise to you: have a look on Carrierwave, Dragonfyl or Paperclip. Thise are awesome FileUploading Gems - fitting your needs.
Look at the documentation of image_tag it expects source of image not collection_proxy You need to iterate the list and show images one by one
<% #guide.guide_pics.each do |pic|%>
<%= image_tag pic %>
<% end %>
Note: pic can be url or name of pic depending on your pics saving technique.
Experienced Java developer, new to Rails - wondering about belongs_to relationship in scaffolding.
Saw another answer like this
Does rails scaffold command support generate belongs_to or many to many model middle table migration info?
and followed the rails generate scaffold_controller obj:references pattern.
The index/show page is showing #<MyClass:xxxx> instead of the string I want - is there a method in the target class (parent side of the belongs_to) I need to override to specify the identifier?
Also in the edit view, it looks like it's trying to modify the reference as a string rather than as drop-down - is there something I need to specify to make that happen?
Thanks!
BTW - I was able to get similar scaffolding to work in Django and Grails, where the foreign key turned into a drop-down; I'm hoping Rails is equally easy and I'm just missing it.
You can override the #to_s method on the instances, as it is the one being called.
class FooDoodle < ActiveRecord::Base
def to_s
name
end
end
That's when showing a record.
However, when you're actually using the form to set the associations, scaffold will only generate an input field in the view so you can enter the id. You could have a dropdown menu for example, but the options for that dropdown would somehow have to be selected in a manner.
For example, if there are 2000 possible associated records, which ones do you show? Do you show the 2000? Only the first 10? That logic would go into your controller.
So, for example:
class FooDoodlesController < ApplicationController
def edit
#foodoodle = FooDoodle.find(params[:id])
#friends = #foodoodle.possible_friends # or else
end
end
and using select and options_for_select as choices
# _form.html.erb
<%= form_for #foodoodle do |f| %>
<%= f.label :friend %>
<%= f.select :friend, #friends.map{ |p| [p.to_s, p.id] } %>
How can I get the Username from an ID in Rails 3?
In my view I call <%= blog.user_id %> for the ID, but how do I get the Name there?
The Controller is a scaffold default.
Make sure you define the association in your Blog model.
belongs_to :user
And then in your view your can use <%= blog.user.name %>
Generally you want to avoid long chains of dots like post.user.name. Try:
class Post < ActiveRecord::Base
belongs_to :user
delegate :name, :to => :user, :prefix => true
end
Then in your views you can call
#post.user_name
to get the users name. I thought I would throw this out there since its good habit I am trying to include in my code as well.
You should use <%= blog.user.name %> and have defined
belongs_to :user
in your Blog model. You should work with ..._id on the view-level.
you should learn how to code in Rails, this is a very basic question.
Consider having a look at http://railsforzombies.org
It's a great online tutorial
Try using <%= blog.user.try(:name) %>
Based on following models
class Company < ActiveRecord::Base
has_and_belongs_to_many :origins
end
class Origin < ActiveRecord::Base
has_and_belongs_to_many :companies
end
I want to have in my companies/_form a collection of checkboxes representing all origins.
Don't know if the Company.new(params[:company]) in companies_controller#create can create the association between company and the selected origins?
I'm running rails 3.0.0, what is the best way to achieve that?
thanks for your insights
habtm isn't a popular choice these days, it's better to use has_many :through instead, with a proper join model in between. This will give you the method Company#origin_ids= which you can pass an array of origin ids to from your form, to set all the associated origins for #company. eg
<% current_origin_ids = #company.origin_ids %>
<% form_for #company do |f| %>
<label>Name:<%= f.text_field :name %></label>
<% Origin.all.each do |origin| %>
<label><%= origin.name %>
<%= check_box_tag "company[origin_ids][]", origin.id, current_origin_ids.include?(origin.id) %>
</label>
<% end %>
<% end %>
As an aside, using a proper join model, with corresponding controller, allows you to easily add/remove origins with AJAX, using create/delete calls to the join model's controller.
I have to agreed with #carpeliam a has_many :through should not be the default choice. A HABTM works fine and involves less code. It also does not restrict the use of ajax and does expose a origin_ids setter to which you can pass an array of ids. Therefore the screencast, whilst from 2007, still works with Rails 3. The other option if using simple_form is this:
= form.association :origins, :as => :check_boxes
Personally I'm not of the belief that has-many-through is always better, it really depends on your situation. Has-many-through is better if there is ANY possibility of your join model having attributes itself. It's more flexible to change. It removes the magic of some Rails conventions. If however you don't need has-many-through, then there's an old RailsCast for HABTM checkboxes that might come in handy.