Circular dependency detected while autoloading constant (ActiveRecord) - ruby-on-rails-4

I have manually specified the relationships in the models (haven't found a way how to automatically generate them from a ERD model or an existing database) and than tried to generate a migration containing the FKs using the immigrant gem. I am getting:
rails generate immigration AddKeys
lib/active_support/dependencies.rb:478:in `load_missing_constant': Circular dependency detected while autoloading constant Assignment (RuntimeError)
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451#railstutorial_rails_4_0/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:180:in `const_missing'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451#railstutorial_rails_4_0/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:512:in `load_missing_constant'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451#railstutorial_rails_4_0/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:180:in `const_missing'
Here is the code for the models:
class Account < ActiveRecord::Base
include Person
include Contact
has_many :coworkers, :class_name => 'Coworker'
has_many :customers, :class_name => 'Customer'
has_many :locations, :class_name => 'Location'
has_many :appointment_types, :class_name => 'AppointmentType'
before_create :create_remember_token
has_secure_password
validates :password, length: { minimum: 6 }
validates :rem_notice_hrs, presence: true
validates :rem_notice_hrs, numericality: true
validates :rem_text, presence: true
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }
after_initialize :init
def Account.new_remember_token
SecureRandom.urlsafe_base64
end
def Account.digest(token)
Digest::SHA1.hexdigest(token.to_s)
end
private
def init
if self.new_record?
if self.rem_notice_hrs.nil?
self.rem_notice_hrs = 24
end
if self.rem_text.nil?
if self.company.nil?
self.rem_text = "Dear [customer title: automatic] [customer family name: automatic], this is a reminder of your appointment with %{title} %{family_name} on [date/time]."
else
self.rem_text = "Dear [title] [customer family name], this is a reminder of your appointment with %{company} on [date/time]."
end
end
end
end
def create_remember_token
self.remember_token = User.digest(User.new_remember_token)
end
end
class Location < ActiveRecord::Base
belongs_to :coworker, :class_name => 'Coworker', :foreign_key => :wor_id
belongs_to :appointment, :class_name => 'Appointment', :foreign_key => :app_id
#optional deletion flag:
validates_inclusion_of :deleted, in: [true, false]
end
class Coworker < ActiveRecord::Base
include Person
validates_inclusion_of :deleted, in: [true, false]
belongs_to :account, :class_name => 'Account', :foreign_key => :acc_id
has_many :assignments
end
class Location < ActiveRecord::Base
belongs_to :coworker, :class_name => 'Coworker', :foreign_key => :wor_id
belongs_to :appointment, :class_name => 'Appointment', :foreign_key => :app_id
validates_inclusion_of :deleted, in: [true, false]
end
class Appointment < ActiveRecord::Base
belongs_to :customer, :class_name => 'Customer', :foreign_key => :cus_id
belongs_to :location, :class_name => 'Location', :foreign_key => :loc_id
belongs_to :appointment_type, :class_name => 'AppointmentType', :foreign_key => :app_type_id
has_many :assignments, :class_name => 'Assignment'
has_many :reminders, :class_name => 'Reminder'
validates_date :from, :on_or_after => :today
validates_date :till, :on_or_after => :from
validates_inclusion_of :deleted, in: [true, false]
attr_accessor :title
end
Some of the models above implement the following concerns:
module Contact
extend ActiveSupport::Concern
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
# phone attribute
included do
validates :phone, presence: true
validates :phone, numericality: true
attr_accessor :company
end
#email regex
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
end
module Person
extend ActiveSupport::Concern
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
included do
# deleted flag attribute
attr_accessor :first_name, :family_name, :title
end
end
Can you tell what's going wrong here?

This is a classical example of a copy-paste mess - I've copied the code of Location.rb into Assignment.rb, edited it but forgot to re-name the class. So I end up having two Location models - in 2 different rb files. Doh!

Related

RoR variable doesn't hold associated records

New to RoR, here couldn't find the solution I was looking for so I'm typing my problem here:
I have a web app where after the user is logged in they will be redirected to a dashboard that shows them their various information.
For now - as I'm getting the feel of RoR - the view of the dashboard was suppose to get a lot of information:
def index
#logged_user = Person.includes(:user, :addresses).find(session[:user_id])
end
but when I checked using #logged_user.inspect it only gave me this
#<Person (person model attributes here) >
I checked the log and it seems the User and Addresses were loaded
printscreen of my log http://prntscr.com/c40fee
but they aren't in the #logged_user variable... did I miss a step here? where did I go wrong?
EDIT:
here's the model of Person
class Person < ActiveRecord::Base
validates :first_name, :last_name, presence: true
has_one :user, :dependent => :destroy
has_one :medical_record
has_many :addresses, :dependent => :destroy
has_many :contacts,:dependent => :destroy
has_many :enrollments_as_student, :class_name => 'Enrollment', :foreign_key => :student_id, :dependent => :destroy
accepts_nested_attributes_for :addresses, allow_destroy: true
accepts_nested_attributes_for :contacts, allow_destroy: true
accepts_nested_attributes_for :medical_record, allow_destroy: true
end
model for User
class User < ActiveRecord::Base
validates :username, presence: true
validates :username, :password, format: {with: /\A[a-zA-Z0-9 ñÑ]+\Z/, message: "Special characters aren't allowed"}
validates :username, uniqueness: {message: "username is already taken"}
has_secure_password
belongs_to :person
end
model for Addresses
class Address < ActiveRecord::Base
validates :street, :barangay, :city, :province,
presence: {message: "Fill in all necessary fields"}
validates :zipcode,
numericality: {message: "Zipcodes should only contain numbers"},
length: {minimum: 4, maximum: 4, message: "Invalid zipcode length"}
belongs_to :person
end
also snippet for the login
def login
#user = User.find_by_username(params[:receptions][:username])
if #user && #user.authenticate(params[:receptions][:password])
session[:user_id] = #user.person_id
flash[:notice] = "You have succfully logged in"
redirect_to '/dashboards'
else
flash[:notice] = "Invalid username or password"
redirect_to '/login'
end

Routing Error Using Rails Favorites, Polymorphic

Following a tutorial for adding favorites to my existing project, whereby users can favorite property listings but can't get past the error below:
Routing error,uninitialized constant FavoriteRoomsController
favoriterooms_controller.rb
class FavoriteRoomsController < ApplicationController
before_action :set_room
def create
if Favorite.create(favorited: #room, user: current_user)
redirect_to #room, notice: 'Room has been favorited'
else
redirect_to #room, alert: 'Something went wrong...*sad panda*'
end
end
def destroy
Favorite.where(favorited_id: #room.id, user_id: current_user.id).first.destroy
redirect_to #room, notice: 'Room is no longer in favorites'
end
private
def set_room
#room = Room.find(params[:room_id] || params[:id])
end
end
room.rb
class Room < ActiveRecord::Base
belongs_to :user
has_many :photos
has_many :favorites
geocoded_by :address
after_validation :geocode, if: :address_changed?
validates :home_type, presence: true
validates :room_type, presence: true
validates :accommodate, presence: true
validates :bed_room, presence: true
validates :bath_room, presence: true
validates :listing_name, presence: true, length: {maximum: 50}
validates :summary, presence: true, length: {maximum: 500}
validates :address, presence: true
validates :lister_type, presence: true
validates :gender_type, presence: true
validates :occupation_type, presence: true
validates :move_in, presence: true
validates :term, presence: true
validates :term, presence: true
end
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :omniauthable
validates :fullname, presence: true, length: {maximum: 50}
has_many :rooms
has_many :favorites
has_many :favorite_rooms, through: :favorites, source: :favorited, source_type: 'Room'
def self.from_omniauth(auth)
user = User.where(email: auth.info.email).first
if user
return user
else
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.fullname = auth.info.name
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.image = auth.info.image
user.password = Devise.friendly_token[0,20]
end
end
end
end
favorite.rb
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :favorited, polymorphic: true
end
routes.rb
Rails.application.routes.draw do
root 'pages#home'
devise_for :users,
:path => '',
:path_names => {:sign_in => 'login', :sign_out => 'logout', :edit => 'profile'},
:controllers => {:omniauth_callbacks => 'omniauth_callbacks',
:registrations => 'registrations'
}
resources :users, only: [:show]
resources :rooms
resources :photos
resources :conversations, only: [:index, :create] do
resources :messages, only: [:index, :create]
end
resources :favorite_rooms, only: [:create, :destroy]
end
roomshow.html.erb
favorites button link code added in my roomsshow.html.erb
<%- unless current_user.favorite_rooms.exists?(id: #room.id) -%>
<%= link_to 'Add to favorites', favorite_rooms_path(room_id: #room), method: :post %>
<%- else -%>
<%= link_to 'Remove from favorites', favorite_room_path(#room), method: :delete %>
<%- end -%>
favorite_rooms_path POST /favorite_rooms(.:format) favorite_rooms#create
favorite_room_path DELETE /favorite_rooms/:id(.:format) favorite_rooms
Screenshot of error message with full trace
I've watched various tututorials and followed numerous suggestions but dont seem to be able to solve the issue by myself.
Routing error,uninitialized constant FavoriteRoomsController
Rails follow naming conventions very strictly and for a good reason. It expects the file names to be in a snake case. So you should change the file name favoriterooms_controller.rb to favorite_rooms_controller.rb

Duplicate join table records being created with has_many checkboxes, can't uncheck/destroy them

I am following the HABTM Railscast #17 on using checkboxes to create associated records using a join table.
I finally got my records to save to the join table....but I can't control them by checking or unchecking a box....meaning unchecking the record does nothing and leaving the record checked makes ANOTHER duplicate record.
I have 3 tables.
:project has_many :restrictions, :through => :project_restrictions
What I'd like to be able to do is have an array of checkboxes that I can check to create :project_restrictions...or, if I uncheck them, the remove the :project_restriction record.
Right now it just keeps saving multiple records and does not delete them if I uncheck one.
I have put all my logic in the ProjectsController and am running the method for adding the :project_restrictions through a custom PATCH method called "add_restrictions". Should this be a POST? I can't figure out if I'm PATCHING Project by adding associated records or POSTING just the associated records.
my join table has an :id and no :timestamps .... I don't know if this matters....obviously, I'm new.
I am using rails 4.
MY MODELS
class Project < ActiveRecord::Base
has_many :project_restrictions, dependent: :destroy
has_many :restrictions, :through => :project_restrictions
accepts_nested_attributes_for :project_restrictions, allow_destroy: true, :reject_if => :all_blank
class Restriction < ActiveRecord::Base
has_many :projects, :through => :project_restrictions
has_many :project_restrictions, dependent: :destroy
class ProjectRestriction< ActiveRecord::Base
belongs_to :restriction
belongs_to :project
end
**My PATCH Controller Method to Create Associated Records **
def add_restrictions
#diet = Restriction.k1.order(:name)
#project = Project.find(params[:p])
params[:project][:restriction_ids].reject!(&:empty?).each do |restriction|
#proj_rule = ProjectRestriction.create!(:project_id => #project.id, :restriction_id => restriction)
#proj_rule.save
end
respond_to do |format|
format.html { redirect_to t1s3_path(:p => #project.id ) ,
notice: 'Success! You added restrictions!' }
format.json {}
end
end
And my form
<%= form_for #project, url: add_restrictions_path(:p => #project.id) do |f| %>
<div class=" fields ">
<%= hidden_field_tag "project[restriction_ids][]", nil %>
<% #diet.each do |restriction| %>
<%= check_box_tag "project[restriction_ids][]", restriction.id,
#project.restriction_ids.include?(restriction.id), id: dom_id(restriction)%>
<%= label_tag dom_id(restriction), restriction.name %><br>
<% end %>
</div>
<%= f.submit %>
UPDATE:
I have updated my Models to have "uniq and inverse_of ....however I'm still creating duplicate records and unable to destroy them by unchecking the checkbox
class Project < ActiveRecord::Base
has_many :project_restrictions -> { uniq }, dependent: :destroy, inverse_of: :project
has_many :restrictions, -> { uniq }, through: :project_restrictions
accepts_nested_attributes_for :project_restrictions, allow_destroy: true, :reject_if => :all_blank
class Restriction < ActiveRecord::Base
has_many :projects, -> { uniq }, through: :project_restrictions
has_many :project_restrictions, -> { uniq }, dependent: :destroy, inverse_of: :restriction
class ProjectRestriction< ActiveRecord::Base
belongs_to :restriction, -> { uniq }, inverse_of: :project_restrictions
belongs_to :project, -> { uniq }, inverse_of: :project_restrictions
end
change this in your add_restrictions method and try
def add_restrictions
#diet = Restriction.k1.order(:name)
#project = Project.find(params[:p])
params[:project][:restriction_ids].reject!(&:empty?).each do |restriction|
#projectrestrictions = ProjectRestriction.all
#projectrestrictions.each do |prj|
if prj.restriction_id == restriction
ProjectRestriction.destroy_all(prj.id)
else
#proj_rule = ProjectRestriction.create!(:project_id => #project.id, :restriction_id => restriction)
#proj_rule.save
end
end
respond_to do |format|
format.html { redirect_to t1s3_path(:p => #project.id ) ,
notice: 'Success! You added restrictions!' }
format.json {}
end
end

rails 4 with nested attributes and nested form

I am trying to model my first rails 4 project to make the leap from tutorial to the real world.
The app is a horse show signup.
The user (devise) adds an 'entry'.
The entry can consists one or more 'rides'
Each ride can have a single 'horse' and a single 'rider' (nested forms would allow user to select existing horse and rider or create new one via form during new entry create.
I started to do some nested web forms with Cocoon and Formtastic:
https://github.com/nathanvda/cocoon
buy I have problems with saving related data to tables.
When I save my entry the rides table contains correct values for:
test, entry_id, but not horse_id
The entry table has no ride_id value. It only has user_id (I set in controller manually)
My questions are:
I am sure I am missing a few conceptual concepts but do I have my models associations set up correctly?
How do I edit my current forms to save data correctly?
When I debug in the controller I get what looks to be an odd param value:
{"utf8"=>"√", "authenticity_token"=>"xxxxxxxxxxxxxx=", "entry"=>{"show_date"=>"10/26/2014", "rides_attributes"=>{"1407930343404"=>{"test"=>"Intro B", "horses"=>{"name"=>"Plain Horse", "_destroy"=>""}}}}, "commit"=>"Create Entry", "action"=>"create", "controller"=>"entries"}
I thought I would see 'horses_attributes' within the 'rides_attributes' hash but instead I see just 'horses'.
Any clarification including link to examples would be very appeciated.
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :entries
has_many :rides, :through => :entries
has_many :horses, :through => :entries
has_many :riders, :through => :entries
end
class Entry < ActiveRecord::Base
belongs_to :user
has_many :rides, :dependent => :destroy
has_many :horses, :through => :rides
has_many :riders, :through => :rides
accepts_nested_attributes_for :rides, :allow_destroy => true
accepts_nested_attributes_for :horses
accepts_nested_attributes_for :riders
end
class Ride < ActiveRecord::Base
belongs_to :entry
belongs_to :user
has_one :horse #, :dependent => :destroy
has_one :rider
accepts_nested_attributes_for :horse
accepts_nested_attributes_for :rider
end
class Horse < ActiveRecord::Base
belongs_to :ride # :dependent => :destroy
belongs_to :user, :dependent => :destroy
end
class Rider < ActiveRecord::Base
#belongs_to :ride #, :dependent => :destroy
belongs_to :user, :dependent => :destroy
has_many :rides
end
Here is my partial schema:
create_table "entries", force: true do |t|
t.date "show_date"
t.integer "user_id"
t.integer "ride_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "entries", ["ride_id"], name: "index_entries_on_ride_id", using: :btree
add_index "entries", ["user_id"], name: "index_entries_on_user_id", using: :btree
create_table "horses", force: true do |t|
t.string "name"
t.string "horse_ncdcta"
t.string "owner_fname"
t.string "owner_lname"
t.date "coggins_date"
t.integer "ride_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
add_index "horses", ["ride_id"], name: "index_horses_on_ride_id", using: :btree
create_table "riders", force: true do |t|
t.string "first_name"
t.string "last_name"
t.string "rider_ncdcta"
t.boolean "senior"
t.boolean "liability_signed"
t.integer "ride_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "riders", ["ride_id"], name: "index_riders_on_ride_id", using: :btree
create_table "rides", force: true do |t|
t.string "test"
t.integer "entry_id"
t.integer "rider_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "horse_id"
end
add_index "rides", ["entry_id"], name: "index_rides_on_entry_id", using: :btree
add_index "rides", ["rider_id"], name: "index_rides_on_rider_id", using: :btree
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "first_name"
t.string "last_name"
t.string "phone"
end
partial entry controller:
def new
#user=current_user
#entry=Entry.new
end
def create
#entry = Entry.new(entry_params)
#entry.user_id=current_user[:id]
respond_to do |format|
if #entry.save
# example of params value
# {"utf8"=>"√", "authenticity_token"=>"xxxxxxxxxxxxxx=", "entry"=>{"show_date"=>"10/26/2014", "rides_attributes"=>{"1407930343404"=>{"test"=>"Intro B", "horses"=>{"name"=>"Plain Horse", "_destroy"=>""}}}}, "commit"=>"Create Entry", "action"=>"create", "controller"=>"entries"}
debugger
format.html { redirect_to #entry, notice: 'Entry was successfully created.' }
format.json { render :show, status: :created, location: #entry }
else
format.html { render :new }
format.json { render json: #entry.errors, status: :unprocessable_entity }
end
end
end
def entry_params
# added ids based on comments below
params.require(:entry).permit(:show_date, rides_attributes: [:id, :test, :_destroy, horses_attributes: [:name, :id]] )
end
views:
_form.html.haml
= semantic_form_for #entry do |f|
= f.inputs do
= f.label :show_date, "Show Date"
= f.input :show_date, :as => :select, :collection => [['08/03/2014', '08/03/2014'], ['09/14/2014', '09/14/2014'], ['10/26/2014', '10/26/2014'], ['11/15/2014', '11/15/2014']]
%h3 Rides
#rides
= f.semantic_fields_for :rides do |ride|
= render 'ride_fields', :f => ride
.links
= link_to_add_association 'add ride', f, :rides
= f.actions do
= f.action :submit
_ride_fields.html.haml
.nested-fields
= f.inputs do
= f.label :test, "Test"
= f.input :test, :as => :select, :collection => [['Intro A', 'Intro A'], ['Intro B', 'Intro B'], ['Intro C', 'Intro C']]
= f.semantic_fields_for :horses do |horse|
= render 'horse_fields', :f => horse
= link_to_add_association 'add horse', f, :horse
_horse_fields.html.haml
.nested-fields
=f.inputs do
= f.input :name
.links
= link_to_add_association 'add horse', f, :horse
Here's what I think is happening, you're nesting horse within Rides instead of Entry. Ride accepts nested attributes for a has_one horse relationship so you're seeing 'horses'. If you submitted horses against the Entry (which has a has_many relationship) you'd see horses_attributes.
In this section of _form.html.haml you assigned ride to f
= render 'ride_fields', :f => ride
And then in your _ride_fields.html.haml you use that f to create a nested form which is being created off the ride variable (Ride has_one horse).
= f.semantic_fields_for :horses do |horse|
= render 'horse_fields', :f => horse
Because your Ride model has accepts_nested_attributes_for :horse you're seeing horses within the Ride. To see horses_attributes you'll want to create horses against the Entry (which accepts the nested attributes for "horses").
I believe you can fix it by changing the render like this (pass in the Entry model form):
= render 'ride_fields', :f => ride, :e => f
And then the horse nested form like this (from f to e):
= e.semantic_fields_for :horses do |horse|
= render 'horse_fields', :f => horse
When using nested attributes the symbol you pass to form_for has to match the association name. You are doing
f.semantic_fields_for :horses
But rides doesn't have a horses assocation, only a horse association. Because of this rails doesn't realise that you're trying to use nested attributes at all, which is why the data is nested under the key "horses" rather than "horses_attributes". You will of course need to change the permitted params to match.
In addition you don't need all the stuff to manage adding extra horse fields for a ride if a ride can in fact only have one horse. If this was a mistake, then leave your forms alone but change the model so that a ride has_many :horses and accepts_nested_attributes_for :horses
I am now saving relationships correctly with below code (with a one caveat). Thanks to previous posters for getting me headed in the right direction!
Also Formtastic doc link: http://rdoc.info/github/justinfrench/formtastic/Formtastic/Inputs/SelectInput
The caveat: I cannot figure out how to select an existing horse with form select if I do not want to add a new horse (_ride_fields.html.haml). Adding new horse works fine.
When I attempt with current commented code I get error:
undefined method ride_id for Ride
I am not sure if this a formtastic problem, model related, or controller. I will post this as a separate question if I do not figure it out.
I realized in my original posting that I should not have foreign keys (eg ride_id) saved to 'entries' table because they are saved to the related tables (rides) in entry_id field.
entries controller (partial):
class EntriesController < ApplicationController
before_filter :set_horses, :except => [:destroy, :index]
def new
#c_user=current_user[:id]
#user=current_user
#entry = current_user.entries.new
#horses = current_user.horses
end
def create
#entry = current_user.entries.new(entry_params)
respond_to do |format|
if #entry.save
format.html { redirect_to #entry, notice: 'Entry was successfully created.' }
format.json { render :show, status: :created, location: #entry }
else
format.html { render :new }
format.json { render json: #entry.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_horses
if current_user.admin?
#horses=Horse.all
else
#horses = current_user.horses
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def entry_params
params.require(:entry).permit(:show_date, rides_attributes: [:id, :test, :_destroy, horse_attributes: [:name, :id]] )
end
end
entry.rb:
class Entry < ActiveRecord::Base
belongs_to :user
has_many :rides, :dependent => :destroy
has_many :horses, :through => :rides, :dependent => :destroy
has_many :riders, :through => :rides, :dependent => :destroy
accepts_nested_attributes_for :rides, :reject_if => lambda { |a| a[:test].blank? }, :allow_destroy => true
accepts_nested_attributes_for :horses, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
accepts_nested_attributes_for :riders, :allow_destroy => true
validates_presence_of :show_date
end
ride.rb:
class Ride < ActiveRecord::Base
belongs_to :entry, inverse_of: :entry
belongs_to :user, inverse_of: :user
has_one :horse
has_one :rider
accepts_nested_attributes_for :horse
end
horse.rb:
class Horse < ActiveRecord::Base
belongs_to :ride, :dependent => :destroy
belongs_to :user, :dependent => :destroy
end
_form.html.haml
= semantic_form_for #entry do |f|
= f.inputs do
= f.input :show_date, :as => :select, :collection => ['2014/08/03', '2014/09/14', '2014/10/26', '2014/11/15']
%h3 Rides
#rides
= f.semantic_fields_for :rides do |ride|
= render 'ride_fields', :f => ride
.links
= link_to_add_association 'add ride', f, :rides
= f.actions do
= f.action :submit
_ride_fields.html.haml
.nested-fields
= f.inputs do
= f.label :test, "Test"
= f.input :test, :as => :select, :collection => [['Intro A', 'Intro A'], ['Intro B', 'Intro B'], ['Intro C', 'Intro C']]
= if !#horses.empty?
-# both of next 2 lines give error: undefined method ride_id for <Ride:0x00000008cc3370>
-#= f.input :horse, :as => :select, :collection => #horses
-#= f.input :horse, :as => :select, :member_label => :name
= f.semantic_fields_for :horse do |horse|
= render 'horse_fields', :f => horse
.links
= link_to_add_association 'add new horse', f, :horse
_horse_fields.html.haml
.nested-fields
= f.inputs do
= f.input :name

Rails test deprecation warning

Rails test introduced the following warning when I was testing my model. What modifications should I make to my model,
rake test test/models/user.rb
[RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.
DEPRECATION WARNING: The following options in your User.has_many :incoming_friends declaration are deprecated: :conditions. Please use a scope block instead. For example, the following:
has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment'
should be rewritten as the following:
has_many :spam_comments, -> { where spam: true }, class_name: 'Comment'
. (called from <class:User> at /home/divya/projects/shufflejoy/app/models/user.rb:9)
and this is my user model,
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :user_name , :email, :first_name ,:last_name , :presence => true
has_many :invitations
has_many :incoming_friends , :class_name => "User" , :foreign_key=>"friend_id" , :through => :invitations, :conditions => ["status = 1"]
has_many :outgoing_friends , :class_name => "User" , :foreign_key=>"user_id", :through => :invitations, :conditions => ["status = 1"]
end
You need to write your association conditions as callable objects. Like so:
has_many :incoming_friends, -> { where(:status => '1') }, :class_name => "User", :foreign_key => "friend_id", :through => :invitations
has_many :outgoing_friends, -> { where(:status => '1') }, :class_name => "User", :foreign_key => "user_id", :through => :invitations
More info at the official Rails docs: http://guides.rubyonrails.org/association_basics.html#scopes-for-has-many