I'm looking to create a "related posts" section, but cheating a little by just displaying the next 6 posts in the series on my show.html.erb
How would I go about showing these?
Thanks,
Jon
EDIT - ADDED POST MODEL
class Post < ActiveRecord::Base
has_and_belongs_to_many :categories
belongs_to :user
is_impressionable :counter_cache => true, :column_name => :view_count
acts_as_taggable_on :tags
extend FriendlyId
friendly_id :title, use: :slugged
scope :featured, -> { where(:featured => true) }
scope :recent, -> { order(created_at: :desc) }
scope :hot, -> { order(view_count: :desc) }
scope :longest, -> { order(duration: :desc) }
def self.sort_by(sort_param)
case sort_param
when 'recent'
recent
when 'hot'
hot
when 'longest'
longest
else
recent
end
end
end
You didn't post the code for your Post model, but assuming it has a posted_at attribute and #post is currently displayed, you can do:
Post.where.not(id: #post.id).order('posted_at desc').limit(6)
Related
I want to test following controller with RSpec
coupons_controller.rb:
class Api::V1::CouponsController < ApiController
def index
if params[:profile_id]
#coupons = Profile.find(params[:profile_id]).coupons
end
end
end
I want to know
1) How to create factories with FactoryBot (spec/factories/profiles.rb, coupons.rb, coupon_profiles.rb)
2)How to write spec/controllers/coupons_controllers.rb:
associations
profile.rb
class Profile < ApplicationRecord
accepts_nested_attributes_for :coupon_profiles
end
coupon.rb
class Coupon < ApplicationRecord
has_many :coupon_profiles
end
coupon_profile.rb
class CouponProfile < ApplicationRecord
belongs_to :coupon
belongs_to :profile
end
Something like:
factory :profile, class: 'Profile', do
...
end
factory :coupon, class: 'Coupon' do
...
end
factory :coupon_profile, class: 'CouponProfile' do
coupon
profile
end
Honestly, your best bet is reviewing the GETTING_STARTED README for FactoryBot -- everything you want to know is in there, with examples.
For your controller specs, have you reviewed the Rspec Controller Specs Documentation? You should be able to do something like:
describe 'CouponsController' do
subject(:request) { '/index', params: params }
shared_examples_for 'success' do
it do
request
expect(response).to have_http_status(:success)
end
end
describe '#index' do
context 'empty params' do
let(:params) { {} }
it_behaves_like 'success'
end
context 'profile_id present' do
let(:params) { { profile_id: profile.id } }
let!(:profile) { coupon_profile.profile }
let(:coupon_profile) { create :coupon_profile }
it_behaves_like 'success'
end
end
end
I want to impliment something which is similar to Twitter Repost System, therefore I will use this as an example. So let's say I have a Tweet Model and I want to allow other user to repost a certian tweet of another user, how do I impliment something like this?
I thought I would be a cool idea to put the retweet class into the tweet to be able to acess the repost too when I use Tweet.all to recive all tweets stored in the database, but somehow I didn't worked as expected...
The following Code is just an example which should show how to impliment this even if it is not working...
Any ideas how I could build a working repost model which also allows me to access both tweets and retweet by using Tweet.all?
class Tweet
class Retweet
include DataMapper::Resource
belongs_to :user, key => true
belongs_to :tweet, key => true
end
include DataMapper::Resource
property :text, String
property :timestamp, String
belongs_to :user
end
Important: I should be carrierwave compatible.
class Tweet
include DataMapper::Resource
property :id, Serial
has n, :retweets, 'Tweet', :child_key => :parent_id
belongs_to :parent, 'Tweet', :required => false
belongs_to :user
def is_retweet?
self.parent_id ? true : false
end
end
original = Tweet.create :user => user1
retweet = Tweet.create :parent => original, :user => user2
retweet.is_retweet? # => true
hi im new in ROR development im just wondering why my app rise a
"undefined method `menu'
i seems to associate my models right
i would like to show a menu that the reservation reserverd and show its recipes inside that menu but it rises undefiend method 'menu'
package_line_item.rb
belongs_to :menu
belongs_to :reservation
reservation.rb
has_one :reservation_package
belongs_to :service
has_many :reservation_function_rooms
has_many :package_line_items
has_many :menus , :through => :package_line_items, :uniq => true
has_many :function_rooms, :through =>:reservation_function_rooms
menu.rb
has_many :package_line_items
has_many :menu_recipes
has_many :recipes, :through => :menu_recipes, :uniq => true
belongs_to :menu_category
package_line_item_controller.rb
def index
#package_line_items = PackageLineItems.all
end
def show
#reservation = Reservation.includes(:package_line_items => :menu).find(params[:id])
end
def new
#reservation = Reservation.find(params[:reservation_id])
#package_line_item = #reservation.package_line_items.build
end
def create
#reservation = Reservation.find(params[:reservation_id])
#reservation.package_line_items.build(params[:package_line_item])
if #package_line_item.save
redirect_to #reservation ,:notice => "added menu"
end
routes.rb
resources :services
resources :reservations do
resources :reservation_packages
resources :reservation_function_rooms
resources :packages
resources :package_line_items
resources :package_crews
end
resources :function_rooms
resources :crews
resources :menu_categories
resources :menus do
resources :menu_recipes
end
ActiveAdmin.routes(self)
devise_for :admin_users, ActiveAdmin::Devise.config
resources :recipe_categories
resources :recipes
package_line_item/show.html.erb
<p id="notice"><%= notice %></p>
<%= #reservation.package_line_items.menu.name%>
if other file is needed feel free to ask me thank you more power to us thanks
It might be because "menu" is a method in the ActiveAdmin DSL. I had a problem with a model called "Page" precisely for this reason. Try renaming your model and see what happens.
I have this code for creating a topic and post in a forum application in Rails 3.1:
def create
#topic = Topic.new(:name => params[:topic][:name], :last_post_at => Time.now)
#topic.forum_id = params[:topic][:forum_id]
#topic.user = current_user
if #topic.save
#post = Post.new(:content => params[:post][:content])
#post.topic = #topic
#post.user = current_user
#post.save!
...
When posting to the create method via the corresponding form, the topic and the post are created and both save calls are successful.
When I call the create method via a functional test, the topic is saved but the post has validation errors.
ActiveRecord::RecordInvalid: Validation failed:
app/controllers/topics_controller.rb:23:in `create'
test/functional/topics_controller_test.rb:26:in `block in <class:TopicsControllerTest>'
The test looks like this:
test "should create topic" do
post :create, :topic => {:name => "New topic", :forum_id => forums(:one).id}, :post => {:content => "Post content"}
end
(current_user is logged in via a setup method.)
When I display the errors of the post object via the debugger or with #post.errors.full_messages, the error array is empty.
The Post model looks like this:
class Post < ActiveRecord::Base
attr_accessible :content
belongs_to :topic
belongs_to :user
end
And the Topic model:
class Topic < ActiveRecord::Base
belongs_to :user
belongs_to :last_poster, class_name: 'User'
attr_accessible :name, :last_poster_id, :last_post_at
belongs_to :forum
has_many :posts, :dependent => :destroy
end
How can I find out what is causing the validation error?
The problem was that I used mocha's Post.any_instance.stubs(:valid?).returns(false) in a test that was executed before my failing test.
Apparently, you have to restore the original behavior before proceeding with other tests by calling Post.any_instance.unstub(:valid?).
I am trying to create a query that finds all the Posts that belong to the same Topic id. I believe I'm on the right track, but all #posts returns is every post in the database.
Topics controller:
def show
#topic = Topic.find(params[:id])
#title = #topic.name
#posts = Post.where('topic' == #topic).order("updated_at").page(params[:page]).per(10) #not working. still just fetches all posts
respond_with(#posts)
end
Topic model:
class Topic < ActiveRecord::Base
has_many :posts, :dependent => :destroy
attr_accessible :name
end
Post model:
class Post < ActiveRecord::Base
belongs_to :topic, :touch => true
accepts_nested_attributes_for :topic
attr_accessible :name, :title, :content, :topic, :topic_attributes
end
I would recommend you to use the association in the model to get all the posts. you could do it like this:
def show
#topic = Topic.find(params[:id])
#title = #topic.name
#posts = #topic.posts.order("updated_at").page(params[:page]).per(10)
respond_with(#posts)
end
You can use the ActiveRecord association to do this:
def show
#topic = Topic.find(params[:id])
#title = #topic.name
#posts = #topic.posts
respond_with(#posts)
end
And if you are going to use 'where' you should use it like this:
Post.where('topic_id' => #topic.id)
This is because topic refers to the activerecord association. But how its stored in the db level is different.
whats inside where is 'almost' sql.