has_many through rails - ruby-on-rails

Country Model
set_table_name "countries"
has_many :states, :primary_key => 'col1', :foreign_key => 'col1'
has_many :cities, :through => :states, :primary_key => 'col1', :foreign_key => 'col1'
State Model
set_table_name "strain_appendices"
belongs_to :country
has_many :states, :primary_key => 'col2', :foreign_key => 'col2'
City Model
set_table_name "ssu_accessions"
belongs_to :country
belongs_to :state
View
<% #countries.each do |country| %>
<%= country.high %>
<% country.states.each do |state| %>
<%= state.high %>
<% country.cities.each do |city| %>
<%= city.high %>
<%= country.high %> and <%= state.high %> give nice output. But, <%= city.high %> shows error as "uninitialized constant Country::City". Where may be the problem? Anybody help me?

Unless you define a method named high in City model or there's a high attribute, you'll get this error.
Maybe you're looking for city.country.high or city.state.high.
Edit
I'm sorry, I misread! As Shadwell said, you'd get another error in the situation described above.

In the State model, you are missing this:
has_many :cities

Related

Join Query on 2 tables(Rails 4)

I having the following in my show.html.erb:
<% if #doctor.referrals_as_from.count > 0 %>
<% #doctor.referrals_as_from.each do |referral| %>
<%= referral.to_id %>
<% end %>
<% end %>
This works fine, giving me a list of matching id numbers from my referral model.
But, rather than getting a list of id numbers I would like to cross reference the "full_name" column from the Doctors model by using the identified id in referrals,basically an inner join. What is the most elegant way of doing this? Add a new method to the controller and to do a joins or includes, or is there a simpler way?
Models:
doctor.rb
class Doctor < ActiveRecord::Base
self.primary_key = "npi"
has_many :referrals_as_from, :class_name => 'Referral', :foreign_key => 'from_id'
has_many :referrals_as_to, :class_name => 'Referral', :foreign_key => 'to_id'
end
referral.rb
class Referral < ActiveRecord::Base
belongs_to :doctor
end
Use this.
<% if #doctor.referrals_as_from.count > 0 %>
<% #doctor.referrals_as_from.each do |referral| %>
<%= referral.doctor.full_name %>
<% end %>
<% end %>
The Referral model actually has two doctors associated with it, as defined by to_id and from_id. So you might need to do the following:
referral.rb
class Referral < ActiveRecord::Base
belongs_to :to_doctor, :class_name => "Doctor", :primary_key => "to_id", :foreign_key => "npi"
belongs_to :from_doctor, :class_name => "Doctor", :primary_key => "from_id", :foreign_key => "npi
end
Then the code becomes
referral.to_doctor.full_name

Rails 3.2: multiple nested data from one model

How to create form and action for multiple nested attributes if:
LineItem:
has_many :item_options, :dependent => :destroy
has_many :product_options, :through => :item_options
ProductOption:
belongs_to :product
belongs_to :option
has_many :item_options
has_many :line_items, :through => :item_options
ItemOption:
attr_accessible :line_item_id, :product_option_id
belongs_to :line_item, :foreign_key => "line_item_id"
belongs_to :product_option,:foreign_key => "product_option_id"
When I'm creating new LineItem, I need to create new ItemOption(s). This is my form:
<%= form_for(LineItem.new) do |f| %>
<%= f.hidden_field :product_id, value: #product.id %>
<%= f.fields_for :item_options do |io| %>
<% #product.options.uniq.each do |o| %>
<%= o.name %>:
<%= io.collection_select :product_option_id, o.product_options.where(:product_id => #product.id), :id, :value %>
<% end %>
<%= f.submit %>
<% end %>
When I'm clicking on Add To Cart, I've get:
ItemOption(#70296453751440) expected, got Array(#70296430421140)
When Adding accepts_nested_attributes_for :item_options to LineItem, my selects not diplayed :(
With
<%= select_tag "product_option_id", options_from_collection_for_select(o.product_options.where(:product_id => #product.id), :id, :value) %>
#item_options not created:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/WM5/MqPn1yCxjKWoJQmjfko2pR4RiYV0S2KeTTpA3w=", "line_item"=>{"product_id"=>"1"}, "product_option_id"=>"5", "commit"=>"add"}
And last one, I've create action like this:
#line_item = LineItem.new(params[:line_item])
#line_item.item_options.build
....
Where am I wrong? :( I'm totally confused.
ps. similar question Rails 3.2 has_many through form submission
This is form:
Looks this line:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/WM5/MqPn1yCxjKWoJQmjfko2pR4RiYV0S2KeTTpA3w=", "line_item"=>{"product_id"=>"1"}, "product_option_id"=>"5", "commit"=>"add"}
The parameter product_option_id is outside line_item hash, and will be inside. Maybe you need write the select like this:
<%= select_tag "line_item[product_option_id]", options_from_collection_for_select(o.product_options.where(:product_id => #product.id), :id, :value) %>
I'm not sure, but maybe is this. Maybe I need more information, like the exact line where is failing.
Extra, the :foreign_key => "line_item_id" and :foreign_key => "product_option_id" are not necesary, because, the belongs_to model name is the same and will use these foreign_key. From api.
Specify the foreign key used for the association. By default this is
guessed to be the name of the association with an “_id” suffix. So
a class that defines a **belongs_to :person** association will use
“person_id” as the default :foreign_key. Similarly, belongs_to
:favorite_person, :class_name => "Person" will use a foreign key of
“favorite_person_id”.
Edit
Sorry, the unknown attribute: product_option_id is because the attribute name is product_option_ids, and is an array, not a unique value. For a has_many relationship, the column name is collection_singular_ids, and the select should be:
<%= select_tag "line_item[product_option_ids][]", options_from_collection_for_select(o.product_options.where(:product_id => #product.id), :id, :value) %>
This should work, I think :)...

Rails has many through relationships

I am using rails has_many through options and not sure if I am doing something wrong here. I would like for a player to create a season and when a player is about to create a season it will show a select menu of all the seasons I have created in years/new. So far that part has worked great but when a player try's to save the season rails does not save it. I am not sure if my association is correct or if I am doing something wrong? Is there any reason why this is not working?
error
No association found for name `season_year'. Has it been defined yet?
season.rb
class season < ActiveRecord::Base
belongs_to :player
has_many :quarters
has_many :years , :through => :quarters
attr_accessible :season_year_attributes
accepts_nested_attributes_for :season_year
end
quarter.rb
class Quarter < ActiveRecord::Base
belongs_to :player
belongs_to :year
belongs_to :season
end
year.rb
class Year < ActiveRecord::Base
attr_accessible :season_year, :season_name
has_many :quarters
has_many :seasons, :through => :quarters
end
player.rb
class player < ActiveRecord::Base
has_many :seasons, :through => :quarters
has_many :years, :through => :quarters
end
_season-form.html.erb
<%= form_for(#season) do |f| %>
<div class="field">
<%= f.label :season %>
<%= f.text_field :season_name %>
</div>
<%= f.fields_for :years do |year| %>
<%= select("season", "year_ids", Year.all.collect {|p| [ p.season_year, p.id ] }, { :include_blank => true }) %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Based on your models, I believe you need to change this:
accepts_nested_attributes_for :season_year
to this:
accepts_nested_attributes_for :years
When you accept nested attributes it is for a Model and not a property of a model (season_year is an attribute of the year model versus accepting the nested attributes for the actual model, Year).
EDIT:
In the Season model, I added year_ids to the attr_accessible expression:
attr_accessible :season_year_attributes, :year_ids
I also altered the season form so that the output of the select list for years was only this:
<%= select("season", "year_ids", Year.all.collect {|p| [ p.title, p.id ] }, { :include_blank => true }) %>
You don't have any season_year in your Season class, that is the answer.
Did you intend to have this as an association?

Has Many Through Checkboxes Not Saving Values Rails3

I've set up a has_many through relationship in my db and changed the keys because I'm working with a legacy db.
It all seems to be working but I cannot get the checkboxes to save to the database.
My models are as follows:
class Radcheck < ActiveRecord::Base
set_table_name 'radcheck'
attr_accessible :attribute_name, :username, :value, :op, :groupname
belongs_to :raduser
has_many :radusergroup, :dependent => :destroy, :primary_key => :username, :foreign_key => :groupname
has_many :radgroupcheck, :through => :radusergroup
end
class Radgroupcheck < ActiveRecord::Base
set_table_name 'radgroupcheck'
has_many :radusergroup, :dependent => :destroy#, :primary_key => :groupname, :foreign_key => :username
has_many :radcheck, :through => :radusergroup
end
class Radusergroup < ActiveRecord::Base
belongs_to :radcheck, :foreign_key => 'groupname', :primary_key => 'username'
belongs_to :radgroupcheck, :foreign_key => 'username', :primary_key => 'groupname'
end
In my form, I have this:
<% Radgroupcheck.all.each do |group| -%>
<%= check_box_tag :groupnames, group.id, #radcheck.radgroupcheck.include?(group), :username => 'radcheck[groupname][]' -%> | <%= label_tag :groupnames, group.groupname -%>
<% end -%>
The form renders the checkboxes ok and I can see the groupnames but nothing happens when I hit save.
The development log is clear and I can't see anything in the mysql query log either.
Try using this in your form:
<% Radgroupcheck.all.each do |group| -%>
<%= check_box_tag "radcheck[radgroupcheck_ids][]", radgroupcheck.id, #radcheck.radgroupchecks.include?(radgroupcheck), :id => "radcheck_radgroupcheck_id_#{radgroupcheck.id}" -%>
<%= label_tag "radcheck[radgroupcheck_ids][]", radgroupcheck.name, :for => "radcheck_radgroupcheck_id_#{radgroupcheck.id}" -%>
<% end %>
And in your controller:
def create
params[:radcheck][:radgroupcheck_ids] ||= []
#radcheck = Radcheck.new(params[:radcheck])
# rest of your controller logic
end
This is adapted from what worked for me in a Rails 3.1 project, based on http://railscasts.com/episodes/17-habtm-checkboxes?view=asciicast

Assigning a nested attribute with Formtastic

I've been trying to figure this one out for a while but still no luck. I have a company_relationships table that joins Companies and People, storing an extra field to describe the nature of the relationship called 'corp_credit_id'. I can get the forms working fine to add company_relationships for a Person, but I can't seem to figure out how to set that modifier field when doing so. Any ideas?
More about my project: People have many companies through company_relationships. With that extra field in there I am using it to group all of the specific relationships together. So I can group a person's Doctors, Contractors, etc.
My models:
Company.rb (abridged)
class Company < ActiveRecord::Base
include ApplicationHelper
has_many :company_relationships
has_many :people, :through => :company_relationships
Person.rb (abridged)
class Person < ActiveRecord::Base
include ApplicationHelper
has_many :company_relationships
has_many :companies, :through => :company_relationships
accepts_nested_attributes_for :company_relationships
company_relationship.rb
class CompanyRelationship < ActiveRecord::Base
attr_accessible :company_id, :person_id, :corp_credits_id
belongs_to :company
belongs_to :person
belongs_to :corp_credits
end
My form partial, using formtastic.
<% semantic_form_for #person do |f| %>
<%= f.error_messages %>
<% f.inputs do %>
...
<%= f.input :companies, :as => :check_boxes, :label => "Favorite Coffee Shops", :label_method => :name, :collection => Company.find(:all, :conditions => {:coffee_shop => 't'}, :order => "name ASC"), :required => false %>
So what I would like to do is something like :corp_credit_id => '1' in that input to assign that attribute for Coffee Shop. But formtastic doesn't appear to allow this assignment to happen.
Any ideas on how to do this?
Are you looking for something like
<% semantic_form_for #person do |form| %>
<% form.semantic_fields_for :company_relationships do |cr_f| %>
<%= cr_f.input :corp_credit_id %>
<% end %>
It is in the documentation

Resources