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
Related
I have a simple rails application with a table created using the rails generate scaffold command. It allows the user to add the names of different movies using a form i.e. automatically generated by rails. I want to add an option where a User can add the name of a director like Christopher Nolan and then add the name of all the movies of that director within it, sort of like a group in the rows. Also all the subrows need to be linked to each other. Is this possible on rails?
P.S. I know that Stack Overflow works on the concept where I add a snippet of code for help, but since I'm a newbie I don't have any idea about how to even start with this
About nested model form http://railscasts.com/episodes/196-nested-model-form-part-1
And this gem can help u for create form with list items
https://github.com/nathanvda/cocoon
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
I'm want to implement twitter-like hashtag on my app.
Let's say I have user input to a text area "I'm coming to #london from #paris" then I want to build tag cloud of the hashtags.
I'm thinking of using acts-as-taggable-on. So I find all of my hashtags from the text in the backend, then save it to tag field.
Anyone has experience on this they would like to share? Thanks.
Yes, ActsAsTaggableOn should be fine. As #etang alluded to, it's a heavy gem, but it gets the job done. If you're looking for a simple way to extract tags from text, you may want to look at https://github.com/twitter/twitter-text-rb. It has some nice regexps that may save you some time.
Twitter::Extractor.extract_hashtags("my #favorite #site is http://foo.com/home#boo")
That would return "favorite" and "site" but not "boo" (as would be expected).
For late-comers…
I have written a simple gem for precisely for this:
https://github.com/ralovely/simple_hashtag
Give feedback or contribute if you feel like it.
ActsAsTaggableOn should work fine if you are not too worried about scaling. It keeps track of your actual tags by ActsAsTaggableOn::Tag, and keeps track of the many-to-many relationship to your posts by ActsAsTaggableOn::Tagging. It also uses polymorphic association in ActsAsTaggableOn::Tagging so you can tag in different namespaces.
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.
I have this quiz rails app linked to an IRC bot who asks questions (yes, on IRC), where I have this Question model which contains, well, questions, answers, hints, and a few more things.
I started with Quiz model (like, say, the special Halloween or Christmas quiz) with a quiz_id in the questions table, then, I told myself, that it would be nice to be able to categorize the questions, so I added a Category model (like, say, Movies or Books), with a category_id in the questions.
Now, my users would like to be able to add a question to one or more quiz, and to assign one or more categories to questions…
So, I've been thinking about removing the Quiz and Category models and replace them with tags, so that, there will be a halloween tag, a movie tag, and a question can have "halloween movie christmas" for tags.
In my searchs, I've seen quite a few ways to include tags like acts_as_taggable, acts_as_taggable_on_steroids or whatever else someone has imagined :-)
Now, I'm wondering what I should do, and so, I'm asking, what you have done, how you've done it, why you did it this way.
acts_as_taggable_on_steroids is older, but it still works, and is still being maintained. Be sure you're visiting the primary repository for it:
http://svn.viney.net.nz/things/rails/plugins/acts_as_taggable_on_steroids/
You can use acts_as_taggable_on_steroids with named_scopes, here's an article that shows how:
Speed Up and named_scope acts_as_taggable_on_steroids Finds
The actual addition to your model is pretty trivial (this is directly from the article above):
named_scope :tagged_with, lambda { |tags| YourModel.find_options_for_find_tagged_with(tags) }
I think Category can easily be replaced by tags, because they are both used to add metadata to a question.
However, I have my doubts about the Quiz model. If you only use the Quiz model to group your questions, it could be possible to replace it with tags. But I think the Quiz model will include more functionality than just grouping alone. For example, score keeping, picking the next question, etc. So I would keep the Quic model intact.
To implement tagging, acts_as_taggable_on_steroids does work, but is a bit old. It would be nice to have a tagging plugin that uses named_scope in the background, as it would make the plugin far more flexible.