2

In my controller I am trying to run a query to get all of the id's not referenced in another table like so:

@vlan_numbers = ActiveRecord::Base.connection.execute("SELECT pop_vlans.id, vlan_number FROM pop_vlans WHERE pop_vlans.id NOT IN (SELECT logical_interfaces.vlan_id FROM logical_interfaces) AND pop_id != " + @pop_id.to_s)

Then in my view I am trying to use collection_select to show these in a dropdown menu:

but the error I get is undefined method 'vlan_number' for [2, "2"]:Array where those values are just the first row of results from the query.

This is a diagram of the two tables involved:

logical_interfaces | pop_vlans
-------------------|-----------
     vlan_id-------|----->id
       ....        |  vlan_number

and the relationships in the models are:

pop_vlan.rb

belongs_to :logical_interface

logical_interface.rb

# no relationship defined

Update

This is how the form is generated:

<%= form_tag :controller => "circuit", :action => "update" %>
    # other inputs
    <%= select_tag options_for_select(@vlan_numbers) %>
    # other inputs
 </form>
0

2 Answers 2

4

You can use select with options_for_select(@vlan_numbers) instead of collection_select

results from ActiveRecord::Base.connection.execute doesn't get loaded into a model

Instead you could try YourModelName.find_by_sql(...) If you want to play with your model

UPDATE Assuming the name of your attribute you want this select_tag to populate is vlan_id so:

<%= form_tag :controller => "circuit", :action => "update" %>
  # other inputs
  <%= select_tag 'vlan_id', options_for_select(@vlan_numbers) %>
  # other inputs
</form>
Sign up to request clarification or add additional context in comments.

7 Comments

thanks but using <%= select options_for_select(@vlan_numbers) %> gives the following error message - wrong number of arguments (1 for 3)
or select_tag may be… depends on how you're building your form if you are using form_for then f.select :vlan_id, options_for_select(@vlan_numbers) given that f is the instance of your form builder
do you mean <%= select_tag options_for_select(@vlan_numbers) %>? Using that, my select menu isn't populated with any options.
can you provide us some snippets on how you are generating your form?
@jo3w, I've updated the question, each input is just manually inserted into the view via an appropriate helper function such as text_field or check_box
|
1

ActiveRecord::Base.connection.execute returns Mysql2::Result not array of object as Arel/AR query does.

So it returns array of arrays containing pop_vlans.id, vlan_number. So have to use first or last in your collection_select instead of something.vlan_number

So you have to use options_for_select instead of collection_select like:

options_for_select(@vlan_numbers.collect{|v| v.first}) 

4 Comments

thanks but I'm not quite sure what you mean, do you have an example?
sample output of @vlan_numbers = ActiveRecord::Base.connection.execute("SELECT pop_vlans.id, vlan_number FROM pop_vlans WHERE pop_vlans.id NOT IN (SELECT logical_interfaces.vlan_id FROM logical_interfaces) AND pop_id != " + @pop_id.to_s) could be [[1,3], [2,3], [4,5], [3,1]]. so if you use [[1,3], [2,3], [4,5], [3,1]] in collection_select you have to operate on array so you should not find vlan_number in array
I think I know what you mean now, so, next question would be, how to get the last member of each array? e.g. using your example, 3,3,5,1 but bearing in mind I would need the first member of each array to act as the value of the select.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.