1

I have a table (peas) containing several foreign keys in each row and listing them in it's index view. I have other tables (carrots) that contain the same foreign keys.

From the first table (peas) I want to find the row in the other table (carrots) that contain the same foreign key values, put that in a variable (@p) to be used inside a conditional statement to be displayed in the index view of the first table (peas). What I've created so far is not throwing an error so it's hard to diagnose.

I've tried to change the operand but having no success.

Pea Model (Adding method to Model because there are no database changes)

  def pea
    @p = Carrot.find({ :fk_id => 'pea.fk_id', :fk2_id => 'pea.fk2_id' })
  end

Corresponding Peas Index View (Inside an each loop)

<% if @p.present?  %>
 <p>Say This</p>
<% else %>
 <p>Say That</p>
<% end %>

Once I've found the row that matches the two foreign keys (fk_id & fk2_id) I'll know that the match is unique. In other words there will only be one row in Carrots that contains the same two foreign key values in Peas.

I feel like I'm missing something really obvious. I've searched similar questions and can't quite match this scenario enough to hack a solution together.

Pea belongs_to :Carrot
Carrot has_one :Pea

Any help in the right direction is appreciated.

RoR - Rails 4.2.5.2 / ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin14], Sqlite3 (dev), Postgresl (Prod), Mac

Phil_ish
  • 95
  • 1
  • 1
  • 9
  • I'm not sure I understand why you have multiple foreign keys. Shouldn't `Carrot has_many :peas`? – Eric Duminil Dec 21 '16 at 16:12
  • In the context of the app, no, a carrot doesn't have many peas. It's actually completely independent from peas. The foreign keys are from two other tables. I should have been more clear but didn't want to over complicate the question. – Phil_ish Dec 21 '16 at 16:17
  • I don't see any problems with your syntax, nor do I think your problem has anything to do with syntax, yet it features prominently in your subject line (i.e. the single most important part of your question, the part upon which people base their decision to click on your question or not). Can you clarify what the exact problem with Ruby syntax is that you are having? – Jörg W Mittag Dec 21 '16 at 17:16
  • @Jorg, perhaps its the wrong syntax for what I'm trying to achieve, not that the syntax itself is incorrect. If "feels" correct, but I'm not getting to my desired outcome which is to find another row in another table with the same foreign key values as the requesting table. I guess I will try to word my questions better in the future? – Phil_ish Dec 21 '16 at 17:26
  • So both the `peas` and `carrots` tables have `fk_id` and `fk2_id` columns and those columns have the same meaning in both tables. And given a `Carrot` instance, you want to find the `Pea` that has the same `fk_id` and `fk2_id` as that `Carrot`? Or do you have a `Pea` instance and want to find the `Carrot` with the same `fk_id` and `fk2_id` as your `Pea`? – mu is too short Dec 21 '16 at 22:27
  • @mu, I think it's more the later. In the context of my app, the pea instance comes first (or already exists) and have two characteristics, for example, called 'in_season' and 'on_sale'. Then carrots are added, one at a time. I want to find (or match?) the newly created rows of carrots that also have the characteristics 'in_season' and 'on_sale'. If found we "Say This" else we "Say That". This is far from the best example, but since I started with peas and carrots...kinda stuck on this road. I hope this makes sense. Thanks for the input. – Phil_ish Dec 22 '16 at 01:27
  • Why wouldn't something like `Carrot.where(:fk_id => self.fk_id, :fk2_id => self.fk2_id)` work? – mu is too short Dec 22 '16 at 18:29

1 Answers1

1

find only accepts primary_key as an argument. Use find_by for search by other attributes.

def pea
  @p = Carrot.find_by(fk_id: 'pea.fk_id', fk2_id: 'pea.fk2_id')
end

Also find throws an error if it find nothing, so if you see no errors chances are that you haven't called method pea at all.

Nikita Misharin
  • 1,961
  • 1
  • 11
  • 20
  • Although this didn't provide a solution it did force me to look at the issue differently. I'm sure the method wasn't called but still not sure why. My Carrot model mimics real-life but might have been the more complicated way to achieve what I was intending to accomplish. Ended up creating a new column with Carrot data into a table that was already associated with Pea. I could do this and still remain within the apps context. Upvoting because it forced me to look at the solution from a different angle. – Phil_ish Dec 24 '16 at 15:48