My objects are melting on the Rails
April 20, 2007 12:46 PM Subscribe
Rails, Ruby, objects, models and the OO confusion of someone harmed by Goto as a child. Can ye help?
I'm playing about with a Rails app to get my head properly around OO and MVC, but have hit what I'm sure is a fairly simple snag. Could you clear it up? (I'll put this in newspaper terms so everyone knows what I mean)
In one of my controllers, I have an object which is a list of all the active stories in the system, @stories. Some of the stories will be unpublished as yet. I want to go through the list and only send to the view those stories which have been published, or unpublished but written by the current user.
That sort of processing sounds like it should go in the model, so in my controller I have something like (simplified to bare bones):
@authorised = []
for story in @stories
if story.is_authorised_for(story, current_user)
@authorised << story
end
end
@authorised
and in the story model, I have
def is_authorised_for(story, user)
if story.author == user
return true
else
return false
end
end
My question is, why do I have to pass "story" as a parameter over to the model? That seems wrong, but without it I get a "invalid method author for "{the contents of the story's article}:string" error.
What have I goofed on?
posted by bonaldi to computers & internet (9 answers total) 5 users marked this as a favorite
@stories = Stories.findAllPublushedPlusUnpublishedFor(current_user)
posted by dws at 12:59 PM on April 20, 2007