0

I'm using the Monologue gem (rails engine for blogging) and mounting it in my app at /blog, I want to get the posts with a certain tag and display those posts on the home page of my app ('/'). From rails c I can get a tag OR a post by doing Monologue::Tag or Monologue::Post but when I try to do Monologue::Tag.posts.published (like how he has the app getting the posts for a specific tag in source) I get "undefined method". I know it's because it's an engine, I'm just not familiar enough with engines to know what the proper syntax for this is? Any help is much appreciated!

NomNomCameron
  • 419
  • 5
  • 14

1 Answers1

0

You are trying to get the posts for a specific tag right? Then, this is not what you want:

Monologue::Tag.posts.published

You are not specifying any tag here, you are calling the method posts on Monologue::Tag, but that is not a specific tag instance, it is the Monologue::Tag class.

Once you have a specific tag, you will be able to call posts.published on it, or as I see in the source, you can even call posts_with_tag

I guess that in order to try this, you could just get the first tag and then call the method:

Monologue::Tag.first.posts.published

Edit:

As I told you in the comment, your problem was that you were trying to call the method posts to an object that was not of the class Monologue::Tag (your model), but of the class ActiveRecord::Relation (a collection of Monologue::Tag objects).. Doing the first works, because is returning the first instance that was found by the where.

Another approach, if you know that you just want to fetch one instance, is to call find instead of where. Like this:

Monologue::Tag.find_by(name: "Goals")

This will return an instance of Monologue::Tag.

Nobita
  • 23,519
  • 11
  • 58
  • 87
  • What I was trying to show is that I can get a tag by calling: `Monologue::Tag.where(name: "Whatever")` and that returns a tag, but when I do something like: `tag = Monologue::Tag.where(name: "whatever")` and try to do this after: `tag.posts.published` I get undefined method I still get undefined method when trying `Monologue::Tag.first.posts.publushed` According to edge guides I tried something like this in console: `Monologue::Tag class_eval do` `self.posts.published` `end` But it still didn't work – NomNomCameron Aug 23 '14 at 22:55
  • `NoMethodError: undefined method 'posts' for # ` Is what I get when I do: `tag = Monologue::Tag.where(name: "Goals")` `tag.posts` – NomNomCameron Aug 24 '14 at 01:57
  • Try this instead ``tag = Monologue::Tag.where(name:'Goals').first`` notice the first – Nobita Aug 24 '14 at 02:06
  • The ``where`` method doesn't return an object of the class that has defines the ``posts`` method, it teturns (as the error shows) an ActieRecord Relation. That relation contains objects (in your case just one) of the class that knows about the posts method. – Nobita Aug 24 '14 at 03:26
  • Also, if this solved your question, please accept the answer. – Nobita Aug 24 '14 at 03:28
  • I still don't quite understand but I'll look more into it. Thank you so much for answering and explaining why this works! I wish I had enough reputation to upvote this. – NomNomCameron Aug 24 '14 at 04:24
  • I have edited my answer to clarify the ``where`` confusion you have. – Nobita Aug 24 '14 at 17:44