Can some1 help me on syntax with the find() function. I''m trying to pull up a table record, called chart, that has a particular field specified. ie chart has a field called record_id, which i know. How can i find the record that contains chart.record_id. something like chart = Chart.find(:conditions => "record_id = :id") this gives me an error. I''m passing :id, to the controller. Please help!! Thanks, Yngwie -- Posted via http://www.ruby-forum.com/.
> chart has a field called record_id, which i know. How can i find the > record that contains chart.record_id.i''m guessing there is a class Record has_many :charts, class Chart belongs_to :record relashinship(by record_id in the chart table) if so, and you know the id of the Record, you could find the corresponding chart object by putting @record = Record.find(params[:id], :include => :charts) and then take the charts out by @charts = @record.charts which will give you an array of all the charts that belong to a record with an id that is identical to the params[:id] variable. hope it helps, shai -- Posted via http://www.ruby-forum.com/.
shai wrote:> i''m guessing there is a > > class Record > has_many :charts, > class Chart > belongs_to :recordThis is not the case. I have a view, called ''charts'' that joins all tables called ''owners'' with all tables called ''records'' each. I know which record_id i''m looking for. i don''t know the chart.id, so i beleive i need to do the search on all the charts to find the chart that contains the chart.record_id that i know. Yngwie -- Posted via http://www.ruby-forum.com/.
> This is not the case. I have a view, called ''charts'' that joins all > tables called ''owners'' with all tables called ''records'' each. I know > which record_id i''m looking for. i don''t know the chart.id, so i > beleive i need to do the search on all the charts to find the chart that > contains the chart.record_id that i know.is it possible for you to show the appropiate tables and their relashinship? do you have a charts table? i''m not really gathering from the information above...detailed info is well appreciated -- Posted via http://www.ruby-forum.com/.
On 7/11/06, yngwie yngwie <mdalsant@gmail.com> wrote:> > Can some1 help me on syntax with the find() function. > > I''m trying to pull up a table record, called chart, that has a > particular field specified. > > ie > > chart has a field called record_id, which i know. How can i find the > record that contains chart.record_id. > > something like chart = Chart.find(:conditions => "record_id = :id")There is no string interpolation included here. The actual condition passed to the database is WHERE record_id = :id This will not work since the db has no record matching :id You need to put it slightly different in your call @chart = Chart.find( :all, :conditions => ["record_id = ?", :id] ) or on edge rails (I don''t think it''s included yet in gem rails) @chart = Chart.find(:all, :conditions => { :record_id => :id } ) It''s not a good idea to use direct string interpolation since this leaves you open to sql injection. ie "record_id=#{:id}" Hope this helps this gives me an error. I''m passing :id, to the controller. Please> help!! > Thanks, > Yngwie > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060711/20c9ad9b/attachment.html