I have two models, Book and Chapter.
I would like chapters to be accessed by their order in the book rather that their id's
/Books/the-chapter-title/chapters/1
Would it be recommended to do this as I would like to also have the ability to reorder the chapters.
I would like the chapter numbers to move up and down the list using acts-as-list but also access them via their pecking order.
you can add a to_params method in your model if you want to use something other than id, but this usually leads to headaches
check out https://github.com/norman/friendly_id
Related
When dealing with resources (e.g. users) different parts of the rails app refer to them in one of several ways, some capitalized/singular, some lowercase/plural etc. At times this seems logical (e.g. a method for several resources vs. just one) but at other times it seems arbitrary...
Is there any easy way of remembering how to access them from different parts of the app?
Most of the time, you will need to access different models across the app. And you would always access them with singular name with first letter uppercase'd like User, Tweet. Regarding controllers, I don't think so you would ever to access a controller from some other controller.
Remember, if are using raw SQL, and you want to access the table of a model, that would always be in plural and all lower case, like users for User, and tweets for Tweet.
Regarding routes, they are always accessed through lowercase words, and deciding whether singular or plural -- it depends upon the context.
If you are accessing all tweets, the route method will be tweets_path, and if want one tweet, then tweet_path(1) or edit_tweet_path(1) where 1 being the id of the tweet that you want to show or edit.
And for classes: everywhere in Rails, and generally speaking in Ruby, they would always be singular, and uppercase'd.
I have 3 tables a monster and a tag table, and a monster_tag table in between. I want to be able to show all the monsters that share the same tag by reading the monster_tag table.Would it be better if I wrote the feature from scratch, or use the act-as-taggable gem. If I pick the act-as-taggable gem, how would I configure it to use the monster_tag table.
For functionality you described setting up has_many through relationships should be enough.
You can get all monsters that share the same tag with something like Tag.find(name: 'tag').monsters
I am working on a rails application and i have 3 different user types. These users are potentially very different, so i created models for each of them. Now, they should be able to login thru a single form. So basically i want to say something like 'find_by_email("some_email")', but search over all three tables. It seems, though, that Rails expect you to call 'find_by' with a specific model, like Admin.find_by(). Any suggestions?
Try something like this and assuming that that the email is unique across all the tables
[Model1, Model2, Model3].each do |model|
break if model.find_by_email("email#email.com").present?
end
Hopefully this is early in your development, but the current structure may not be the best possible route. How many different columns are NOT shared by each of the user types? You may want to use a "user role" system, and have that simply be an extra column on your user table.
Than, you can use something like CanCan to manage those roles and what/where they may access.
I am new to rails so go easy. I have two tables that I am trying to work with here, 'post' and 'category'.
The 'post' table includes the following columns, title:string content:text category:string.
The 'category' table simply contains name:string.
The idea is that the client can manage the categories and also when adding a new post, they can select from a drop down that references their categories.
What is the best way to accomplish this?
You might want to model the category differently. The usual approach is to create a PostCategory model and controller, and use a relation from posts to PostCategory. Read up on belongs_to and the other rails associations before you get much further into this project. When you're ready to continue, take a look at formtastic, it makes handling the forms for the associations much easier to code
flyfishr64 is right, the "correct" way to do this would be to put the categories in their own model/table.
There's lots of helpers like collection_select that will take your list of categories (PostCategory.all) and make a dropdown list for you with the appropriate name to save it in a specific field.
That said, you could pull a distinct list of the entries in that column already and use that for your dropdown, but it's a lot more hassle than just making a model for the category.
Is it possible to use select fields with nested object forms feature of Rails 2.3?
Example:
Suppose you have a Article model, Category model, and a ArticleCategories join model. Article has_many Categories through ArticleCategories.
On our Edit Article form, you want to have an HTML select list of all the available categories. The user can select one or more Category names to assign to the Article (multiple select is enabled).
There are lots of ways to do this, but I'm wondering if there is a simple way to accomplish this using the nested objects feature. What would the form look like in your view?
Check out the nested form example from Github:
http://github.com/alloy/complex-form-examples
It's been a while since I looked at it, so I'm not sure if it covers exactly what you wanna do, but its a nice source for ideas / patterns.
Assuming you have defined the models and their relationships so you can do this:
#art = Article.find(article_id)
#art.categories # returns list of category objects this article is assigned to.
Then I usually use http://trendwork.kmf.de/175
You need to copy the JavaScript file into public/javascripts but after that you can just create the form element with something like:
swapselect(:article,#art,:categories,Category.find(:all).map { |cat| [cat.name, cat.id] })
(I would tend to wrap that in a helper to make the call even cleaner)
One small gotcha is that for very long lists it can run a little slow in IE6 because there's quite a lot of appendChild calls in the js which is notorioulsy slow in IE6
Update: Apologies. This doesn't really answer your original question, which was specifically about the Rails 2.3 feature. The swapselect option is version independent and doesn't make use of newer Rails functionality.