on the main layout for my controller, i need a list of users - each with their own link to a lists of tasks that they have been assigned. i did have a line inside the .rhtml file that found all the users, but i''m guessing this is probably the wrong place to put them. i''m new to ruby and rails, so i still struggle a little with the MVC concept. -- Posted via http://www.ruby-forum.com/.
I would think that putting the find command in your model would be the best place. On Tue, 2006-04-25 at 20:32 +0200, Josh Kieschnick wrote:> on the main layout for my controller, i need a list of users - each with > their own link to a lists of tasks that they have been assigned. > > i did have a line inside the .rhtml file that found all the users, but > i''m guessing this is probably the wrong place to put them. i''m new to > ruby and rails, so i still struggle a little with the MVC concept. >Charlie Bowman www.recentrambles.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060425/c62ba95a/attachment-0001.html
Charlie Bowman wrote:> I would think that putting the find command in your model would be the > best place.when putting something in the model, does it have to be within a "def action_name" block, or can it be defined anywhere? -- Posted via http://www.ruby-forum.com/.
Yes, I would create a method in the model to find the list of users. I would then call the method from within my controller. Here''s a little sudo code. Model: def find_users ## put your sql or find statement here end Controller @users = User.find_users Charlie bowman www.recentrambles.com On Tue, 2006-04-25 at 21:06 +0200, Josh Kieschnick wrote:> Charlie Bowman wrote: > > I would think that putting the find command in your model would be the > > best place. > > when putting something in the model, does it have to be within a "def > action_name" block, or can it be defined anywhere? >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060425/0ad2da11/attachment-0001.html
On Apr 25, 2006, at 03:20 PM, Charlie Bowman wrote:> Yes, I would create a method in the model to find the list of > users. I would then call the method from within my controller. > Here''s a little sudo code. > > Model: > def find_users > ## put your sql or find statement here > end > > Controller > @users = User.find_usersQuick correction here: if you want to use the method in this manner, where you don''t have an existing instance of the User class that you are performing the method on, you have to define the method as a class method, not an instance method, like so: def self.find_users ... end Btw, this is exactly what I recommend doing, especially in those cases where the kind of find you want to perform is one you will do in more than one location and the complexity of the find is more than you can do with a simple dynamic finder: User.find_all_by_firstname_and_lastname(params[:firstname], params [:lastname]) -Brian