I have two tables, accounts and items and I would like to show the buisness_name instead of the idfrom the accounts table on the view/items/show.html.erb page.
Currently I have no associations between the models, but I have the account_id column in the items table.
create_table "accounts", force: :cascade do |t|
t.string "buisness_name"
t.string "web_site"
t.string "phone_number"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "items", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "image"
t.decimal "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
t.json "attachments"
t.integer "account_id"
end
I'm getting the account_id via this: <%= #item.account_id %>, but I would like to show the buisness_name instead.
Try something like this
class Item < ActiveRecord::Base
belongs_to :account
end
Into the view
<% if #item.account %>
<%= #item.account.buisness_name %>
<% end %>
Should be business_name, as #Sergio Tulentsev already told you
I updated my answer because I noticed from the table that the account_id has not a not null constraint
If you have
<%= #item.account_id %>
The horrible way to get the account would be
<%= Account.find_by(id: #item.account_id).try(:buisness_name) %>
Much smarter would be
class Item
belongs_to :account
delegate :buisness_name, to: :account, prefix: true, allow_nil: true
And then in the view...
<%= #item.account_buisness_name %>
Related
So I have a scaffold for both blogs and post_category.
I made an association with both of them. Here's my schema:
create_table "blogs", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug"
t.integer "status", default: 0
t.bigint "post_category_id"
t.index ["post_category_id"], name: "index_blogs_on_post_category_id"
t.index ["slug"], name: "index_blogs_on_slug", unique: true
end
create_table "post_categories", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
So simply I created a couple of post categories and when I try to make an association with blog items so whenever I create a new blog I can display a select statement and choose my preferred category for the blog item I am trying to create but I don't know how to display all categories on all forms and on the index.html.erb file:
<div class="field">
<%= form.label :category %>
<%= form.collection_select :post_category, PostCategory.all %>
</div>
How can I achieve this? And make sure that it saves the data as well?
as per this reference, the format is
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
and for your code, it can be like this
<%= form.collection_select(:post_category, :post_category_id, PostCategory.all, :id, :name, prompt: true) %>
update 2:
inside your blog_controllers
# white list parameter
def blog_params
params.require(:blog).permit(
:post_category_id,
... others fields
)
end
I have a joined table but looking for a way to input the information from a form into both tables, or have it work in general:
My schema:
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "categories_listings", id: false, force: :cascade do |t|
t.integer "category_id", null: false
t.integer "listing_id", null: false
end
create_table "listings", force: :cascade do |t|
t.string "name"
t.text "description"
t.decimal "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image"
t.integer "user_id"
end
Models:
class Category < ApplicationRecord
has_and_belongs_to_many :listings
end
Listing < ApplicationRecord
belongs_to :category, required: false
belongs_to :categories_listings, required: false
end
Views
<%= form_with(model: listing, local: true) do |form| %>
...
<div class="space">
<%= form.select :category_ids, options_from_collection_for_select(Category.all, :id, :name), :prompt => "Select a Category", :multiple => true %>
</div>
...
Before i joined the tables, I had it working with a categories element (i believe thats the right term) within the listing tables that was attached to a categories table... You can see my previous post on SO where I was suggested to do this: Allowing multiple records in category to submit to listing
When i click submit, nothing enters into the categories_listings tables. Suggestions on how I make this happen?
The associations in your Listing model are wrong. It should be just
Listing < ApplicationRecord
has_and_belongs_to_many :categories
end
I suggest you to read has_and_belongs_to_many
I'm new to Rails so maybe this is a stupid question. I'm struggling to get a attribute from another model in a nested form. Let me explain:
I have 3 models linked each other. Poi -> PoiDescription <- DescriptionType
One Poi can have multiple descriptions and a description have only one description type. I'm creating the PoiDescriptions inside the Poi form with a nested form. Every thing is working well, but now, inside the fields_for I want a label before the textarea with the name of the description type. But I don't know how to get it...I can't do something like 'p.description_type.name' so how can I get that attribute?
Here is my code:
Poi Controller
def new
#poi = Poi.new
#descriptions = DescriptionType.all
#descriptions.each do |d|
#poi.poi_descriptions.new(description_type_id: d.id)
end
end
Poi form
<%= form_with model: #poi do |f| %>
...
<div class="col-md-12">
<%= f.fields_for :poi_descriptions do |p| %>
<%= p.hidden_field :description_type_id %>
<%= p.text_area :description %>
<% end %>
</div>
...
Schema
create_table "description_types", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "poi_descriptions", force: :cascade do |t|
t.text "description"
t.bigint "poi_id"
t.bigint "description_type_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["description_type_id"], name: "index_poi_descriptions_on_description_type_id"
t.index ["poi_id"], name: "index_poi_descriptions_on_poi_id"
end
create_table "pois", id: :serial, force: :cascade do |t|
t.text "name"
t.float "longitude"
t.float "latitude"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "monument_id"
t.integer "beacon_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.index ["beacon_id"], name: "index_pois_on_beacon_id"
t.index ["monument_id"], name: "index_pois_on_monument_id"
end
Hope you can help me! Thanks in advance.
To get from the form_builder_object to the actual object you can do p.object.description_type.name
If you just want that as a title and not as an input field you can either:
Add it in a p or span tag and make it look like a label with css or
Add a label_tag with a custom name and title as you need. https://apidock.com/rails/ActionView/Helpers/FormTagHelper/label_tag
I’m developing question and answers application, where a user attempts to answer questions, I am looking to define a custom method rather than doing coding in the controller methods, creating a separate method that can be called to check the answer the user submitted against the answers database table, where the question_id and the user_input is the same as the Answers database.
Can anyone guide me with where I am going wrong or is there a better approach to checking the user submitted input against the database and then inputting the submission into the database.
Submissions form
<%= form_for :submission :controller => :submissions :action => 'check_answer' do |f| %>
<%= f.text_field :contnet, :value => '' %>
<%= f.submit 'Submit Answer' %>
<% end %>
Submissions_controller
def check_answer
answer = Answer.find_by(question_id: params[:question_id][:content])
if answer.present?
#insert into submissions table, display correct and add to scoreboard
else
redirect_to competitions_path
end
end
Answers Table :
create_table "answers", force: :cascade do |t|
t.string "content"
t.integer "question_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Question_table :
create_table "members_questions", force: :cascade do |t|
t.string "title"
t.integer "category_id"
t.integer "point_id"
t.integer "event_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.string "file_file_name"
t.string "file_content_type"
t.integer "file_file_size"
t.datetime "file_updated_at"
end
I'd like to sum quantities by category in the different models (Shop and Item).
models
class Order < ActiveRecord::Base
has_many :shops
end
class Shop < ActiveRecord::Base
belongs_to :order
has_many :items
end
class Item < ActiveRecord::Base
belongs_to :shop
has_one :order, autosave: false, through: :shop
end
There are the columns category and quantity in the both shops and items table as below.
I'd like to sum and display both quantity by category.
schema.rb
ActiveRecord::Schema.define(version: 20160610051929) do
create_table "orders", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "shops", force: :cascade do |t|
t.string "name"
t.integer "order_id"
t.integer "category"
t.integer "quantity"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "items", force: :cascade do |t|
t.string "name"
t.integer "category"
t.integer "quantity"
t.integer "shop_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
orders_controller.rb
class OrdersController < ApplicationController
def show
#orders = Order.find(params[:id])
end
end
view/orders/show.html.erb
<%= render #orders %>
My current view is as follows;
Although it only calculate in items table, I'd like to add the quantity in shops.
view/orders/ _order.html.erb
<% order.shops.each do |shop| %>
<% shop.items.group(:category).sum(:quantity).each do |category, sum| %>
Category <%=category%> : <%= sum %><br>
<% end %>
<% end %>
Although I also tried the following view instead of the above code, it only works when the shop's category exist in item's category.
If there is no category same as shop's category in the item, the result is not what I'd like to.
view/orders/ _order.html.erb
<% order.shops.each do |shop| %>
<% shop.items.group(:category).sum(:quantity).each do |category, sum| %>
<% if shop.category.present? && shop.quantity.present? && category == shop.category %>
<% sum = sum + shop.quantity %>
<% end %>
Category <%=category%> : <%= sum %><br>
<% end %>
<% end %>
It would be appreciated if you could give me how to sum and display both quantities or better way.
SOLVED
I could do what I'd like to do as followings;
schema.rb
create_table "categories", force: :cascade do |t|
t.integer "category"
t.integer "quantity"
t.integer "shop_id"
t.integer "item_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "order_id"
end
create_table "items", force: :cascade do |t|
t.string "name"
t.integer "shop_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "orders", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "shops", force: :cascade do |t|
t.string "name"
t.integer "order_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
orders_controller.rb
class OrdersController < ApplicationController
def show
#orders = Order.find(params[:id])
#categories = Category.where(order_id: params[:id])
end
end
_order.html.erb
<% order.shops.each do |shop| %>
<% #categories.where("order_id = ?", order.id).each do |cate| %>
<%= cate.order_id %>, <%= cate.shop_id%>, <%= cate.item_id%>, <%= cate.category%>, <%= cate.quantity%> <br>
<% end %>
<% #categories.where("shop_id = ?", shop.id).group(:category).sum(:quantity).each do |category, sum|%>
Category <%=category%> : <%= sum %><br>
<% end %>
<% end %>
Your second approach should work, style-wise you can write it much shorter:
sum += shop.quantity.to_i if shop.category == category
nil.to_i is 0, so no matter if shop.quantity is nil or not you can safely add it as long as shop.category == category And if shop.categoryis nil, it won't be equal to category, so there is no need to ask for it's presence either.
That said, I still think you might want to rethink the way your models are.