please help solve the problem.
There is a table users:
id :integer, autoincrement
name :string
gender_id :integer
There is a table genders:
id :integer, autoincrement
gender_name :string
model User:
class User < ActiveRecord::Base
belongs_to :gender
end
model Gender:
class Gender < ActiveRecord::Base
has_many :users
end
I need the gendername. I try to do so:
<% #users.each do |user| %>
<div class="col-xs-6"><%= user.name %></div>
<div class="col-xs-2"><%= user.gender_id %></div>
<% end %>
the result is output number, but I need to gendername
my column 'gender_id' contain zero value. This problem has been
Related
I would appreciate any help. I'm using enum to select the gender between male and female. But is not working, is returning female but not male, it appears empty. And when I go to the edit section it doesn't show male or female as selected.
When I go the rails console and I type gender.enum? it returns true or false.
That's my Customer model
class Customer < ApplicationRecord
enum gender: [:female, :male]
end
That's what I have in the _form.html.erb
<%= radio_button_tag(:gender, "male") %>
<%= label_tag(:gender_male, "Male") %>
<%= radio_button_tag(:gender, "female") %>
<%= label_tag(:gender_female, "Female") %>
The gender was in boolean before, then I changed it to integer in order to use enum
class ChangeGenderData < ActiveRecord::Migration[5.2]
def change
change_column :customers, :gender, :integer
end
end
Here is an image of what appears
I solve the problem using this code.
<%= form.collection_radio_buttons :gender, Customer.genders, :first, :first %>
some fields tables City
create_table "cities", force: :cascade do |t|
t.string "name"
some fields tables Order
create_table "orders", force: :cascade do |t|
t.integer "count"
t.integer "first_city_id"
t.integer "last_city_id"
model Order
class Order < ActiveRecord::Base
belongs_to :city, inverse_of: :city
end
model City
class City < ActiveRecord::Base
has_many :orders, inverse_of: :order
end
working output data from Order in view
<% #order.each do |orders| %>
<p> <%= orders.count %> </p>
<% end %>
Method in controller
def edit
#orders= Order.all
#order=Order.where(:user_id == current_user)
end
How to get the value from the name Sity table if first_city_id or last_city_id in the Order table contains id required entry of City Table?
You should setup the order associations as follows:
class Order < ActiveRecord::Base
belongs_to :first_city, class_name: "City" # it would use first_city_id as the foreign key by default to access city table
belongs_to :last_city, class_name: "City"
end
Getting the first/last city name from an order becomes easy:
#order = Order.first
#order.first_city.name
#order.last_city.name
If you want to get the first/last city name from an order list, you should use includes or joins to avoid the n+1 query:
#orders = Order.includes(:first_city, :last_city)
<% #orders.each do |order| %>
<p> First City: <%= order.first_city.name %> </p>
<p> Last City: <%= order.last_city.name %> </p>
<% end %>
or
#orders = Order.
joins("LEFT JOIN cities AS first_cities ON first_cities.id = orders.first_city_id").
joins("LEFT JOIN cities AS last_cities ON last_cities.id = orders.last_city_id").
select("orders.*, first_cities.name AS first_city_name, last_cities.name AS last_city_name")
<% #orders.each do |order| %>
<p> First City: <%= order.first_city_name %> </p>
<p> Last City: <%= order.last_city_name %> </p>
<% end %>
You can get more information and helps from these two docs "RailsGuides - Active Record Associations" and "RailsGuides - Active Record Query Interface".
I have the following line of code which seems to work okay.
<% current_user.blockedshows.map(&:tvshows).each_with_index do |blocked, index| %>
However, when I call it on blocked.title and image_tag(blocked.image), (full code below) I am getting the following error: undefined method title for <ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Tvshow:0x007fd4e24b9448>
View
<%= blocked.title %>
<%= image_tag(blocked.image) %> </br>
Tweets containing the following keywords will be removed from your timeline: </br>
<%#keywords = blocked.phrases.map(&:text) %>
<%=#keywords %> </br>
Blockedshow Model
class Blockedshow < ActiveRecord::Base
has_many :tvshows
belongs_to :user
end
Tvshow Model
class Tvshow < ActiveRecord::Base
has_many :phrases
belongs_to :blockedshow
end
Tvshow Table
class CreateTvShows < ActiveRecord::Migration
def change
create_table :tvshows do |t|
t.string :title
t.string :image
t.timestamps
end
end
end
That's because tvshows itself is an ActiveRecord association object.
You need to loop over it also:
current_user.blockedshows.map(&:tvshows).each_with_index do |blocked, index|
blocked.each do |b|
<%= b.title %>
end
end
I am trying to show the value of an attribute via its id with formtastic. I have two models hat are setup like this
class Membership < ActiveRecord::Base
has_many :members
attr_accessible :membership_type
end
class Member < ActiveRecord::Base
belongs_to :membership
accepts_nested_attributes_for :membership
attr_accessible :membership_id, :forename, :middlename, :surname, :house_no, :house_name, :street, :town, :postcode, :home_tel, :mobile_tel, :work_tel, :email
end
My index view is set up like this so far
index do
column :forename
column :middlename
column :surname
column :house_no
column :house_name
column :street
column :town
column :postcode
column :home_tel
column :mobile_tel
column :work_tel
column :email
column :membership
default_actions
end
but the value for the membership is output as
#<Membership:0x007f2c2064c370>
How would i get the actual value so it will say "Student" for example
Normally i would do something like this i guess
<% #members.each do |m| %>
<%= m.forname%>
<%= m.surname%>
<% m.memberships.each do |s| %>
<%= s.membership_type%>
<% end %>
but cant figure it out within formtastic
Thanks
ActiveAdmin tries its best to find a display name for your objects using a list of possible attributes:
:display_name
:full_name
:name
:username
:login
:title
:email
To ensure your Membership object has a recognizable name throughout the system you can add one of those attributes as a method to Membership:
class Membership < ActiveRecord::Base
has_many :members
attr_accessible :membership_type
def display_name
membership_type
end
end
Alternatively, you can do like MichaĆ Albrycht suggests and configure views to perform custom rendering for the membership column.
Try this:
column 'Membership type' do |member|
"#{member.membership.membership_type}".html_safe
end
column 'Other members' do |member|
member.membership.members.each() do |m|
"#{m.forname} #{m.surname}<br/>".html_safe
end
end
Thanks #Cmaresh. Problem is solved. This is the commit link where i solved the problem in my project. Any one can see the source code and can easily understand how to solve the problem.
Im trying to create an application were teachers a able to select students each day who aren't in school. I created the models through nifty-generators gem. The problem is that it wont submit to the notpresents table. Please help.
# == Schema Information
#
# Table name: students
#
# id :integer not null, primary key
# name :string(255)
# group_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Student < ActiveRecord::Base
attr_accessible :name, :group_id
belongs_to :days
end
# == Schema Information
#
# Table name: notpresents
#
# id :integer not null, primary key
# student_id :integer
# day_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Notpresent < ActiveRecord::Base
attr_accessible :student_id, :day_id
belongs_to :days
end
# == Schema Information
#
# Table name: days
#
# id :integer not null, primary key
# title :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class Day < ActiveRecord::Base
attr_accessible :title, :presents
has_many :notpresents
accepts_nested_attributes_for :notpresents
end
And view _form.html.erb
<%= form_for #day do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<% for student in Student.find(:all) %>
<div>
<%= check_box_tag :notpresents, student.id%>
<%= student.name %>
</div>
<% end %>
<p><%= f.submit %></p>
<% end %>
I've never used the nifty-generators gem, but if a student can be absent in many days, and a day can have many students absent, shouldn't you have a many-to-many relationship?
class Student < ActiveRecord::Base
attr_accessible :name
has_many :days, through: :notpresents
has_many :notpresent
end
class Days < ActiveRecord::Base
attr_accessible :date
has_many :students, through: :notpresents
has_many :notpresent
end
class :Notpresents < ActiveRecord::Base
attr_accessible :student_id, :day_id
belongs_to :students
belongs_to :days
end
It could also be a has_and_belongs_to_many association, but with a has_many :through you could have a string or text attribute to make a note about the absence or something like that.
I recommend using simple_form for the form, it makes it so easy:
app/controllers/days_controller.rb:
def edit
#day = Day.find(params[:id])
end
app/views/days/_form.html.erb :
<%= simple_form_for #day do |f| %>
<%= f.association :students, as: :check_boxes %>
<% end %>