开发者

retrieve data from database in rails?

I have two tables called events and trainers in my project.events table contain trainer's id. I have drop down menu which 开发者_开发知识库contains trainer's name as follows.

<% form_tag "/events/find_trainer" do %>
<%= collection_select("event", "trainer", @trainers , :id, :name, {:prompt => true}) %>
<%= submit_tag "search" %>
<%end%>

then i created find_trainer method in controller to retrieve data of events table.

def find_trainer
@events=Event.find(:all, :conditions=> { :trainer=>params[:id]})
  end

This will not retrieve anything from the database.i am using postgresql and ruby 1.8.7 and rails 2.3.8 versions. I could not understand my fault.. Can anybody help me?


If you have your associations defined with has_many, you don't need to use find that way

You just find the trainer:

@trainer = Trainer.find_by_id(params[id])

and then can access all it's events with

@trainer.events

For this to work you need in your trainer model:

has_many :events

in your Event model:

belongs_to :trainer

and a trainer_id column in the events table

edit

Naming the foreign_key column 'trainer' is a bad idea, it breaks Rails conventions and makes your life more difficult. You can tell Rails which column name to use like this:

has_many :events, :foreign_key => :trainer

and

belongs_to :trainer, :foreign_key => :trainer

but you should avoid this if any possible and change your migration instead and rebuild your database. This feature is mainly thought to allow using databases from old projects, not written in Rails.

In addition the other errors tell you, that there is no value in params[:id]. Do you have fixture data in the database?

To just see, if it works at all, you could start with

@trainer = Trainer.first

This would take the first trainer and then

@trainer.events

should work, if any events for this trainer exist. Then you can find out what's wrong with params[:id], most likely some link doesn't work properly, maybe your form is wrong or the routing to your controller has errors.

You are trying too many things at once, without having made sure, that the basics work. That's why we normally do test driven development in Ruby on Rails.

First set up the Trainer model. Then test it, make sure there are trainers by writing and loading fixtures, and that Rails can find them.

After that add the events model (and some fixture data again). If you don't explicitly write tests, then at least use the Rails console (i think it was the command script/console in Rails 2.x) to test. Only if you are 100% sure it works on the model level, start writing controllers. That way you have to handle only one error at any given moment.


Foreign keys are usually named after the table at which they point.

Try this?

@events = Event.find(:all, :conditions => {:trainer_id => params[:id]})


The params from this post method will be params[:event][:trainer] this will carry the value of id.

and also the column value of the foreign_key have to check.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜