Ok, so I've got a weird pattern here that I can't figure out.
I have an STI set with CallList as the base model, and City & State inherited. A city belongs to a state (and a state has many cities).
A campaign has many call lists, so I want to display them all. I loop over campaign.call_lists and sometimes get Cities, sometimes States. When I want to link to them I do
link_to call_list.name, call_list
which works fine if I have these routes:
resources :cities, :states
When I nest cities inside states, however, the link_to helper can't figure out the appropriate route. Is there a simple way to do this, or am I going to have to do some manual path helper construction?
you need to check for kind of call_list and then use proper route..it can not be directly as route is nested..
link_to call_list.name, call_list.is_a?(City) ? call_list : state_city_path(call_list)
Related
What is the correct way to make this api 'restful'
I have a location,
GET location/:id
PUT location/:id
The spec has changed and now this location can have certain 'types' the client can update. One requirement is that the client get the allowed types (could be one of these from a fixed list - i.e.['urban', 'wilderness', 'private', etc..]) for all locations. This isn't a nested resource, its a column on the location table. I've implemented,
GET location/types_allowed
is there are better RESTFUL way?
Another example, a location can have neighbors, so I made GET locations/neighbors. I'm having trouble understanding the nuances of REST beyond CRUD and nested resources. Thanks!
Instead of a column as types_allowed, create a type resource and the relation can be like
location.rb
belongs_to :type
type.rb
has_many :locations
and with this your route file would be
resources :type do
resources :location
end
I strongly recommend against naming a model/column "type". That tends to break magic things inside of rails that you don't even know are there until you try that. It's one of those "undocumented features" :P
I'd instead recommend reading up on "single table inheritance" or "polymorphism" to figure out if either of these is right for you.
If not... then name something "kind" instead of "type" as it'll all work a lot better that way.
As to RESTful resources... you really can't go past the Rails Guides. If you follow the routing guide: (and all the other ones) you'll get an idea of how to do RESTful resources
I have user model and boat model. User has many boats. Boat belongs to user. What i wonder is that, even though i did not nest the resources i am able to create a boat to logged in user. So my url becomes ..../boats.new2 (2 as user id) and it actually saves to user with an id number 2.
But as far as i know if i have nested resources it would become something like .../user/2/boats/1. Isn't it?.
I have not tried to #edit action any of the cases so not sure which one to use and their effects, is there any other advantages using any of them, or not nesting is wrong?
Nesting routes is not necessary in Rails. You can maintain the relationship between boats and users as long as you have the active record belongs_to and has_many methods defined in the model, along with the foreign keys in the db.
In general, you should nest resources if there is an obvious relationship between the objects, such as users and posts (or boats). It just makes more sense of your users and as an API structure.
In some cases, you might want to nest the relationship under something different, like a category name. For instance, your uri pattern could look like /sailboats/boats/1 or powerboats/boat/2. Bottom line is you should structure routes in a way that logical for the project's users and developers.
I am trying to get better with Rails and nesting resources. A User has many Event (and vice versa) through UserEvent join table. I also want a User to Comment on an Event. So far, Event is nested under User in my resources. How would you nest Comment? Would that be nested under Event so there are two successively nested resources under User? How would that work?
I think this would be my suggested nested approach:
resources :users
resources :events do
resources :comments
end
In a many-to-many relationship, it doesn't usually make sense to nest one under the other because neither has a stronger containment relationship.
In your scenario, a user doesn't belong to one event and an event doesn't belong to one user, so having your routes represent it like that doesn't quite model the relationship. Your URLs would look like /events/1/users/2. That sort of implies that User 2 only exists in Event 1.
I think it would make sense that your User and Event are top-level resources. Comments, on the other hand, do have some ownership which makes sense to nest. More than likely, a Comment will be associated with the Event in regards to context. The User is simply the person responsible for it. Nesting comments under the user would give you easy URLs to display all the comments for a user, but I'm betting you're more likely to display all the comments for an Event. With that in mind, I would suggest nesting Comments under Events.
This also makes sense if you were to delete the user and nullify the user_id in the Comment model. You could still have a URL for the comment. If you delete the Event, the comments likely aren't useful anymore anyway, so you could simply destroy them.
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.
My (rails 3) application use collection and product as models. collection has_many products and product belongs_to collection.
I managed to have interactions between products and models. I created a menu displaying the different collection. I want to display a view showing only the product belonging to a specific collection.
1) Is it more elegant to create a new view/controller, or do i create a new view in the product views
2)It seems that i must do something with the routes.rb, but how and what?
3)What link_to arguments must i use to pass the value of my collection?
4)I read a whole book (pragmatic ROR) and depspite that and doing a lot of online research i keep ending here asking for not so much complicated Rails question. What am i doing wrong?
I would go with creating new action in collection controllers. Url will look like this:
/collections/1/products
where 1 is collection id.
I assume you have
resources collections
so you need to add 'products' action for collection member:
resources collections do
member do
get :products
end
end
You can run rake routes from console to see how your application routes look now.
Link code should look like this
link_to "Collection products", products_collection_path(#collection)
In my opinion reading is ok, but while you read you should do lots of examples, write them yourself, becouse otherwise you forget stuff very quickly. I'm 100% sure that stuff I wrote above was in the book you've read.