0

I have two models, Circuit and Organisation with the following relationship:

circuit.rb

belongs_to :organisation

organisation.rb

has_many :circuits

circuit_controller.rb

...
if params[:id]
  @circuit = Circuit.find(params[:id])
  @backup_circuits = @circuit.organisation.circuits.where('id != ?', @circuit.id)
end
...

update.rhtml (Circuit View)

<%= collection_select 'circuit', 'backup_circuit_id', @backup_circuits, :id, :product_name %>

but I get this error: undefined methodproduct_name' for " # AND id != ? ":String`

As far as I can see the modelling should be fine, the only thing I'm doubtful about is the chaining I have done in the controller as it seems a bit funny to find the circuit, it's organisation and then other circuits belonging to that organisation.

Rails verions is 2.3.14

Alternatively, if I use

<%= select "circuit", "backup_circuit_id", @backup_circuits %>

instead then I the page renders but my dropdown values are empty and the values are the hex address you get when you know something is broken...

martincarlin87
  • 10,848
  • 24
  • 98
  • 145
  • I guess you passed extra argument for `collection_select` (http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-collection_select). Could you try to remove `'circuit'` from it? – ck3g Aug 21 '13 at 12:10
  • if I do that I get `wrong number of arguments (4 for 5)` but I would need it as that's the object all my form post data will be stored in. – martincarlin87 Aug 21 '13 at 12:13
  • Try to call `#all` on `@backup_circuits`. Looks like it doesn't like to have a relation passed instead of an array. Like `@backup_circuits.all` or `@backup_circuits = @circuit.organisation.circuits.where('id != ?', @circuit.id).all` – Nikita Chernov Aug 21 '13 at 12:30
  • If I try that I then get `undefined method `all' for #` – martincarlin87 Aug 21 '13 at 12:31
  • Are you sure that Rails 2.3.14 supports the `.where()` method? Try `@circuit.organisation.circuits.find(:all, :conditions => ['id != ?', @circuit.id])` – MrYoshiji Aug 21 '13 at 13:22
  • ah, you are right again @MrYoshiji, if you post that as an answer I will accept it. Thanks again. – martincarlin87 Aug 21 '13 at 13:26

1 Answers1

1

I don't think that Rails 2.3.14 supports the .where() - Actually it does not for Rails 2.3.11, just tried it.

In Rails 2.3.14, you can't really chain the queries... Here is the Rails 2.3.x solution:

@circuit.organisation.circuits.find(:all, :conditions => ['id != ?', @circuit.id])
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117