简体   繁体   中英

Finding variables in ruby on rails applications

I have two tables: one called styles, which has 5 attributes (plus id and created_at) and another called recommender, which has 6 attributes (plus id and created_at).

Once I have defined the 5 attributes on the Styles table, I want to be able to show an entry from the recommender table that has 5 attributes matching the attributes in the styles table.

I have tried a few combinations of Model.find and Model.where to get it, but nothing seems to work.

Here is the styles_controller.rb

def show
@style = Style.find(params[:id])
bt = @style.bodytype
n = @style.need
t = @style.texture
c = @style.color
st = @style.statement   
@recommender =  Recommender.with_bodytype(params[:bt]).with_need(params[:n]).with_texture(params[:t]).with_color(params[:c]).with_statement(params[:st])
respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @style }
 end
end

The message I get is: Undefined method `with_bodytype' for #

So, thanks to @bravenewweb for helping out... But there are still some issues:

I think I figured out the problem, but I am not sure how to fix it.

The production environment has Recommender with all its data (I put it there) and so does development.

Recommender exists now at Heroku, but not with any data. And I am not sure how to put it there.

I think the problem is that the Production db is SQLite, even though it is only supposed to work in development and test. So how do I get the production db to talk to Heroku?

Please help!

It should be much easier than that, ActiveRecord's find method will by default return the first record that matches. What you want to do is chain the attribute names with and, then pass the query attributes in using the same order as the method name

@recommender = Recommender.find_by_bodytype_and_need_and_texture_and_color_and_statement(bt, n, t, c, st)

If you want to find all the records matching those params, you just need to start it with

@recommender = Recommender.find_all_by_bodytype_and_need_and_texture_and_color_and_statement(bt, n, t, c, st)

I am not familiar with the .with_<attribute> methods (do they exist?), but it sounds like you want a straightforward where clause. If I understand correctly you want to find all Recommenders who share the attributes of the selected style. So something like

@recommender = Recommender.where("bodytype = :bodytype AND need = :need AND texture = :texture AND color = :color AND statement = :statement", [:bodytype => @style.bodytype, :need => @style.need, :texture = @style.texture, :color => @style.color, :statement => @style.statement]).first

You could also use ? for each of the named placeholders, but when you have lots of conditions, this reads a little more clearly.

Also, this probably isn't a show method, it's more like index since the query could return more than one result, right? If so, maybe your instance variable should be plural ( @recommenders ). Or maybe I am missing something?

如果您希望样式与推荐者唯一关联,则可以将has_one关联具有复合键作为[bodytype,need,texture,color,statement]。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM