Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Pollyanna
Mar 5, 2005

Milk's on them.


Really, the Rails code is not the worst thing about the app. It's the fact that it basically came about from the CTO going "I want this make it!!!" and a six-week hackjob, and now we wanna try and sell it (lol). There's no product management or direction and it's kind of a death march, I've found. Fun :shepface:

Adbot
ADBOT LOVES YOU

necrotic
Aug 2, 2005
I owe my brother big time for this!

aunt jemima posted:

3.0 to 3.2 took 3 developers 4 months. Good luck! :homebrew:

Thats about how long our 2.3 to 3.0 upgrade took. I wish we could skip 3.2 entirely (and we're looking at doing just that), but from what I've read the best path is 3.0 -> 3.2 -> 4.2, which is unfortunate given some performance regressions in 3.2.

KoRMaK
Jul 31, 2012



I'm messing around with Rubocop and wondering: is there a way to specify the rails version of the cops? OR does it pick that up automatically?

PotatoJudge
May 22, 2004

Tell me about the rabbits, George
I'm going through the newest version of Agile Development for Rails and I don't get why this is handled in a migration and not a model, and I don't know where it it actually ending up, it's not creating a trigger in the database.

http://pastebin.com/SKdmikcf

The book covers creating a simple storefront site. You start at a catalog page listing the products available, click an "Add to Cart" which creates a new record in the carts table for that session, and creates a record in the line_items table with the cart ID, product ID and a quantity (set to a default of 1). The migration groups multiple copies of the same product together in the cart for display. It's easy enough to follow, I just don't understand why it is not in a model or what is triggering it.

MasterSlowPoke
Oct 9, 2005

Our courage will pull us through
It looks like that's going through and updating every cart to follow the new policy of grouping LineItems together. It is triggered once, when you run your migration. It's odd that there's no structural changes that enforce or allow the policy in that migration - I would have added the quantity column there instead of an earlier migration if I was writing that tutorial.

I think it might just be a demonstration of how to write a reversible complex migration, and showing that migrations can do more than just alter tables.

Pardot
Jul 25, 2001




The reason they're doing this, I suppose, is they are changing how things are going to be represented for all time going forward. It seems like before there wasn't a quantity column, and now there is, and this makes it as if it was always there.

If instead it was something you'd have to do periodically, they'd put it in the model. It gets triggered when you run `rake db:migrate`

However, it's generally a slightly bad idea to use model methods inside a migration. Often migrations sit around for a while and you and your team mates run them over and over again as you're working. If you later change something like how Cart#line_items works, or rename that method, then this migration will stop working. However, I think it's more common now for set up scripts to use schema.rb or structure.sql files which have the entire schema and all the migrations up to now burned in, so maybe this isn't so much of an issue to use model methods in the migrations these days.

Also for fun, here is maybe how to do it in just sql

code:
=# select * from line_items;
 id | product_id | cart_id | quantity
----+------------+---------+----------
  1 |          1 |       1 |   (null)
  2 |          1 |       1 |   (null)
  3 |          2 |       1 |   (null)
  4 |          2 |       2 |   (null)
  5 |          2 |       3 |   (null)
  6 |          1 |       3 |   (null)
 17 |          1 |       1 |   (null)
 18 |          1 |       1 |   (null)
 19 |          2 |       1 |   (null)
 20 |          2 |       1 |   (null)
(10 rows)

Time: 0.312 ms
=# begin; with old as (delete from line_items returning *),
gr as (select cart_id, product_id, count(product_id) as quantity from old group by cart_id, product_id)
insert into line_items(cart_id, product_id, quantity) (select * from gr);
BEGIN
Time: 0.121 ms
INSERT 0 5
Time: 0.491 ms
***=# select * from line_items;
 id | product_id | cart_id | quantity
----+------------+---------+----------
 21 |          2 |       2 |        1
 22 |          1 |       3 |        1
 23 |          2 |       1 |        3
 24 |          1 |       1 |        4
 25 |          2 |       3 |        1
(5 rows)

kayakyakr
Feb 16, 2004

Kayak is true

Pardot posted:

The reason they're doing this, I suppose, is they are changing how things are going to be represented for all time going forward. It seems like before there wasn't a quantity column, and now there is, and this makes it as if it was always there.

The other alternative would be to locally declare the model inside the migration. That way the model is always guaranteed to be defined and change-resistant.

PotatoJudge
May 22, 2004

Tell me about the rabbits, George

MasterSlowPoke posted:

It looks like that's going through and updating every cart to follow the new policy of grouping LineItems together. It is triggered once, when you run your migration. It's odd that there's no structural changes that enforce or allow the policy in that migration - I would have added the quantity column there instead of an earlier migration if I was writing that tutorial.

I think it might just be a demonstration of how to write a reversible complex migration, and showing that migrations can do more than just alter tables.

You're right! This is what I get for trying to do this stuff at midnight, I totally glossed over the model change that was done a step before. Pardot was right as well, the quantity field was added after the line item table was created. This migration cleans up the mess that was put in place before quantity field and new method in the cart model were added.

Huzanko
Aug 4, 2015

by FactsAreUseless
What's the best or correct way to package front-end assets in Rails?

A book I'm working through uses Bower and the bower-rails gem. However, apparently Bower isn't cool anymore and the cool thing to do is use NPM and WebPack.

I used to just use the asset pipeline and the gem for whatever assets I was trying to import - e.g. bootstrap-sass-official.

KoRMaK
Jul 31, 2012



Just had to debug the mandarin locale file for hosed up dates and month breviations

This


month_names: "[1月,2月,3月,4月,5月,6月,7月,8月,9月,10月,11月,12月]"
abbr_month_names: "[,一月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月]"


needs to be this

month_names: [~, "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
abbr_month_names: [~, "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]


that was a fun experiment in figuring out what mattered and what didn't

e: oh god dammit I can't even paste it here
ee: oh there it goes, had to take it out of code quotes

necrotic
Aug 2, 2005
I owe my brother big time for this!

Noam Chomsky posted:

What's the best or correct way to package front-end assets in Rails?

A book I'm working through uses Bower and the bower-rails gem. However, apparently Bower isn't cool anymore and the cool thing to do is use NPM and WebPack.

I used to just use the asset pipeline and the gem for whatever assets I was trying to import - e.g. bootstrap-sass-official.

Do you want to keep using sprockets? Are you just looking for some simple "frameworks" like bootstrap, nothing like React?

I'd just grab the distribution files (usually non-minified) and toss them into vendor/assets/*, then load through sprockets.

If you are trying to build out a larger front-end I would take it out of rails entirely, and then load the assets from a CDN/asset dev server when local.

edit: Also, the v4 bootstrap gem is all sass and official. No more third party sass gems/sources for bootstrap!

necrotic fucked around with this message at 17:37 on Jun 7, 2016

Huzanko
Aug 4, 2015

by FactsAreUseless

necrotic posted:

Do you want to keep using sprockets? Are you just looking for some simple "frameworks" like bootstrap, nothing like React?

I'd just grab the distribution files (usually non-minified) and toss them into vendor/assets/*, then load through sprockets.

If you are trying to build out a larger front-end I would take it out of rails entirely, and then load the assets from a CDN/asset dev server when local.

edit: Also, the v4 bootstrap gem is all sass and official. No more third party sass gems/sources for bootstrap!

I like sprockets. Normally would just grab the distro files, as you say, and toss them into vendor/assets/*

The book I'm working with is an Angular 1.5 + Rails 4 book. It has you using bower to pull in Bootstrap.

Honestly I'm just sick of all the build tools and package managers. That's one of the reasons I have gone back to Rails from Node lately - it's easy to get up and running without loving with 3 or 4 different bullshit build tools and package managers that make things that used to pretty easy and quick - getting assets and adding script tags - a billion times more difficult and annoying UNTIL you get over the learning curve of whatever bullshit tool is popular this week. I'm even a front-end developer and have been doing front-end for about 10 years now and used to this bullshit and I am starting to really loving hate it. /rant /meltdown

Huzanko fucked around with this message at 18:00 on Jun 7, 2016

Chilled Milk
Jun 22, 2003

No one here is alone,
satellites in every home

Noam Chomsky posted:

I like sprockets. Normally would just grab the distro files, as you say, and toss them into vendor/assets/*

The book I'm working with is an Angular 1.5 + Rails 4 book. It has you using bower to pull in Bootstrap.

Honestly I'm just sick of all the build tools and package managers. That's one of the reasons I have gone back to Rails from Node lately - it's easy to get up and running without loving with 3 or 4 different bullshit build tools and package managers that make things that used to pretty easy and quick - getting assets and adding script tags - a billion times more difficult and annoying UNTIL you get over the learning curve of whatever bullshit tool is popular this week. I'm even a front-end developer and have been doing front-end for about 10 years now and used to this bullshit and I am starting to really loving hate it. /rant /meltdown

Yeah, unless you're worried about separating out the front end, or need absolute bleeding edge assets at all times either use gems or plain rear end vendoring. Throw in libsass to speed up sass compiles.

Huzanko
Aug 4, 2015

by FactsAreUseless
Any compelling reason to use WebPack with Rails?

EVGA Longoria
Dec 25, 2005

Let's go exploring!

Noam Chomsky posted:

Any compelling reason to use WebPack with Rails?

It's becoming an actual decent standard for js. If you're gonna do things outside of rails, it doesn't hurt to be familiar.

That said, bower loving sucks and I've had nothing but pain using it. Npm is better, but only somewhat. I'd stick to asset gems if you are in sprockets.

necrotic
Aug 2, 2005
I owe my brother big time for this!

Noam Chomsky posted:

Any compelling reason to use WebPack with Rails?

If you're going to use WebPack (instead of sprockets) keep it all out of your rails repo. Though maybe there's some sprockets-webpack thing? If I ever get to the point of adding more than misc. JS functions to my rails assets (eg react or angular) I pull it out to a stand-alone repo.

kayakyakr
Feb 16, 2004

Kayak is true

Noam Chomsky posted:

What's the best or correct way to package front-end assets in Rails?

A book I'm working through uses Bower and the bower-rails gem. However, apparently Bower isn't cool anymore and the cool thing to do is use NPM and WebPack.

I used to just use the asset pipeline and the gem for whatever assets I was trying to import - e.g. bootstrap-sass-official.

npm is what's gradually taking over. Bower is awful. Like, kill it with fire awful. npm is OK.

Browserify-rails works pretty well, though. I would look at that first, if you're wanting to roll with new-style packagers and whatnot.

necrotic posted:

If you're going to use WebPack (instead of sprockets) keep it all out of your rails repo. Though maybe there's some sprockets-webpack thing? If I ever get to the point of adding more than misc. JS functions to my rails assets (eg react or angular) I pull it out to a stand-alone repo.

webpack with rails is no where near as mature as something like ember-cli-rails or browserify-rails.

Huzanko
Aug 4, 2015

by FactsAreUseless

kayakyakr posted:

npm is what's gradually taking over. Bower is awful. Like, kill it with fire awful. npm is OK.

Browserify-rails works pretty well, though. I would look at that first, if you're wanting to roll with new-style packagers and whatnot.


webpack with rails is no where near as mature as something like ember-cli-rails or browserify-rails.

Thanks for the comments guys.

Personally I've always been OK with just using gems and sprockets. But, everywhere you turn now it's all about JS build tools for front-end. I'm pro JS dev, so you think I'd enjoy it, but all the setup annoys me. I'd rather just add script tags and get coding but I realize this doesn't work at an organizational level.

At this point for what I'm building it looks like I'll be using rails-api and rack-cors on the server and then building out the client separately since it'll be an SPA and I plan to upgrade from Angulat 1.5 later - so keeping the two halve separate makes some amount of sense and simplifies things for me. Just trying to get something off the ground without having to spend months learning something new beforehand. Tired of learning and never shipping.

kayakyakr
Feb 16, 2004

Kayak is true

Noam Chomsky posted:

Thanks for the comments guys.

Personally I've always been OK with just using gems and sprockets. But, everywhere you turn now it's all about JS build tools for front-end. I'm pro JS dev, so you think I'd enjoy it, but all the setup annoys me. I'd rather just add script tags and get coding but I realize this doesn't work at an organizational level.

At this point for what I'm building it looks like I'll be using rails-api and rack-cors on the server and then building out the client separately since it'll be an SPA and I plan to upgrade from Angulat 1.5 later - so keeping the two halve separate makes some amount of sense and simplifies things for me. Just trying to get something off the ground without having to spend months learning something new beforehand. Tired of learning and never shipping.

Recommend browserify-rails in that case. It's pretty painless to get started in rails. Hooks in with Sprockets. Doesn't play nice with RequireJS, but you shouldn't be mixing the two anyway. Can fairly easily switch to a different package manager later outside the rails app.

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

I'm looking for work if anyone is hiring remote / socal. I'm good at coding and stuff and I write unit tests

Huzanko
Aug 4, 2015

by FactsAreUseless

kayakyakr posted:

Recommend browserify-rails in that case. It's pretty painless to get started in rails. Hooks in with Sprockets. Doesn't play nice with RequireJS, but you shouldn't be mixing the two anyway. Can fairly easily switch to a different package manager later outside the rails app.

Cool. Thanks. I am fairly familiar with Browserify, but, you know, now it's old and bad because Webpack exists. :)

Will check it out.

kayakyakr
Feb 16, 2004

Kayak is true

Noam Chomsky posted:

Cool. Thanks. I am fairly familiar with Browserify, but, you know, now it's old and bad because Webpack exists. :)

Will check it out.

Webpack community should work on integrating it all nice and tidy like Browserify and ember-cli-rails, then. Those jerks.

Huzanko
Aug 4, 2015

by FactsAreUseless

kayakyakr posted:

Webpack community should work on integrating it all nice and tidy like Browserify and ember-cli-rails, then. Those jerks.

I guess that's my issue. I have been spoiled by Rais' convention over configuration and now I want it everywhere. I hate config.

Pollyanna
Mar 5, 2005

Milk's on them.


Is there a plug-and-play solution for tags, like you'd see on Tumblr or somesuch? Otherwise, I'm trying to implement tagging via a join model, but I'm having trouble wrapping my head around handling arbitrary tags (i.e. tags that don't already exist in the database). I'm expecting something like this to represent an update to an existing asset, with tags:

code:
{
  id: 1,
  name: "sample asset",
  tags: [
    { id: 1, name: "sample tag" },
    { id: nil, name: "arbitrary tag" }
  ]
}
but from what I can tell, this fails in @asset.update(asset_params) due to the arbitrary tag not having an ID, and not existing yet. What's the general approach for something like this, where you both update and create a resource at the same time?

edit: I suspect this has something to do with accepts_nested_attributes_for.

Pollyanna fucked around with this message at 19:16 on Jun 8, 2016

Chilled Milk
Jun 22, 2003

No one here is alone,
satellites in every home
You probably want acts-as-taggable-on. It'd work great for something like that.

Used to use it a lot until I got tired of the tweaks needed to get it to work the way we needed. I ended up writing my own tagging gem for the company, which was a fun little exercise.

Pollyanna
Mar 5, 2005

Milk's on them.


The Milkman posted:

You probably want acts-as-taggable-on. It'd work great for something like that.

Used to use it a lot until I got tired of the tweaks needed to get it to work the way we needed. I ended up writing my own tagging gem for the company, which was a fun little exercise.

acts-as-taggable-on is almost what I want, except that I need to be able to tag search-matching and I think that requires matching against every tag in the database. If there's just a single table involved and the SQL isn't any more complicated than SELECT * FROM tags WHERE tags.name LIKE %input%, then it'd work - I'll look into the gem. Thanks!

good jovi
Dec 11, 2000

'm pro-dickgirl, and I VOTE!

Pollyanna posted:

but from what I can tell, this fails in @asset.update(asset_params) due to the arbitrary tag not having an ID, and not existing yet. What's the general approach for something like this, where you both update and create a resource at the same time?

edit: I suspect this has something to do with accepts_nested_attributes_for.

Are you on postgres? Just store the tags in an array field on Asset. You've gotta set up an index on the tags, but it's so much easier to deal with.

EDIT: Or I could actually read your problem. An array would actually be really bad for you, there isn't really an efficient way to get all the tags.

good jovi fucked around with this message at 20:17 on Jun 8, 2016

Huzanko
Aug 4, 2015

by FactsAreUseless
Does anyone have any recommendations for a data model for managing bids? Like for a use-case like Ebay?

My initial inclination is to have a Bid model and use has_many_through to relate a User and an Item, then when an item is purchased the Bid is destroyed or archived in some way.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
That seems like a fine way to deal with the relations, sure.

But bidding systems are really complex and if this thing is supposed to be anything close to real time, you need to not be using rails definitely not active record.

If the bidding system is not serious then you'll be fine but if not you need to stop what you're doing and either spend a while reading or hire people who have specific experience with this.

Huzanko
Aug 4, 2015

by FactsAreUseless

MALE SHOEGAZE posted:

That seems like a fine way to deal with the relations, sure.

But bidding systems are really complex and if this thing is supposed to be anything close to real time, you need to not be using rails definitely not active record.

If the bidding system is not serious then you'll be fine but if not you need to stop what you're doing and either spend a while reading or hire people who have specific experience with this.

So what are some alternative technologies then? Also, define "real time" in this context. Ebay ain't exactly lightning fast and wasn't always as fast as it is today.

Would you suggest something like Node and Mongo as an alternative?

Also, to clarify, the items and bids use case was just used as an example. My actual use case is more like a job board where users bid on a project and the project owner can accept any bid.

Huzanko fucked around with this message at 19:20 on Jun 9, 2016

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Noam Chomsky posted:

So what are some alternative technologies then? Also, define "real time" in this context. Ebay ain't exactly lightning fast and wasn't always as fast as it is today.

Would you suggest something like Node and Mongo as an alternative?

Also, to clarify, the items and bids use case was just used as an example. My actual use case is more like a job board where users bid on a project and the project owner can accept any bid.

I can't really suggest an alternative because it's not something I've implemented -- I've worked with bidding systems like app Nexus though and it's complex stuff.

The question is really 'how much trouble will we get in if the bidding system is wrong and people demonstrably lose bids because of it.' It might be totally fine if it's wrong, 'but it could also be really bad.

Just saying, this isn't something you want to try and figure out on your own from scratch unless it really doesn't matter.

Pollyanna
Mar 5, 2005

Milk's on them.


Hot on the heels of tagging, I'm wondering what the best way to model a has-one/has-many relationship is. I have Assets, and I have Compliances. Each Asset is associated with exactly one Compliance, and each Compliance is associated with many different Assets. I'm wondering where I put belongs_to and has_many. I'm guessing that it goes something like: "An Asset belongs to one Compliance", "A Compliance has many Assets", but that doesn't quite sound right. It's more like "An Asset has one Compliance", "A Compliance belongs to many Assets". Should I maybe model this with some sort of has_one :through relation or join table or something?

edit: Went with the former.

Now I gotta figure out what to go with for API authentication. Since I'm used to Rails' HTML-based auth setup, I need to familiarize myself with how you do that via an API :pwn:

Pollyanna fucked around with this message at 19:06 on Jun 10, 2016

Huzanko
Aug 4, 2015

by FactsAreUseless

Pollyanna posted:

Hot on the heels of tagging, I'm wondering what the best way to model a has-one/has-many relationship is. I have Assets, and I have Compliances. Each Asset is associated with exactly one Compliance, and each Compliance is associated with many different Assets. I'm wondering where I put belongs_to and has_many. I'm guessing that it goes something like: "An Asset belongs to one Compliance", "A Compliance has many Assets", but that doesn't quite sound right. It's more like "An Asset has one Compliance", "A Compliance belongs to many Assets". Should I maybe model this with some sort of has_one :through relation or join table or something?

edit: Went with the former.

Now I gotta figure out what to go with for API authentication. Since I'm used to Rails' HTML-based auth setup, I need to familiarize myself with how you do that via an API :pwn:

has many through?

https://github.com/lynndylanhurley/devise_token_auth for API token auth

Also, check out the rack-cors gem if doing it cross-domain.

Pollyanna
Mar 5, 2005

Milk's on them.


Noam Chomsky posted:

has many through?

https://github.com/lynndylanhurley/devise_token_auth for API token auth

Also, check out the rack-cors gem if doing it cross-domain.

That's how I typically do it, but for some reason this time it just didn't fit the model.

I'll look into the API token auth too, and good tip on rack-cors. It might end up being cross-domain, I don't know yet.

necrotic
Aug 2, 2005
I owe my brother big time for this!
Your relationship problem is solved exactly by the route you went. Think about in terms of the database FKs and PKs: a belongs_to has a foreign key constraint on records, so Asset has a compliance_id column referencing the Compliance PK; a has_many is the inverse of this, where Compliance has no local column referencing the assets it "owns".

Huzanko
Aug 4, 2015

by FactsAreUseless
So I keep hearing Rails is dead and dying. Any truth to that?

8ender
Sep 24, 2003

clown is watching you sleep
I think it's less relevant in the age of API driven JS apps but I've yet to find a JS stack that doesn't become a worse cluster gently caress than Rails for a large app.

Also a lot of devs confuse "mature" with "dying" because the dev community has an attention span measured in months when it comes to web frameworks.

Huzanko
Aug 4, 2015

by FactsAreUseless

8ender posted:

I think it's less relevant in the age of API driven JS apps but I've yet to find a JS stack that doesn't become a worse cluster gently caress than Rails for a large app.

Also a lot of devs confuse "mature" with "dying" because the dev community has an attention span measured in months when it comes to web frameworks.

I'm a full-time frontend developer who picked up Rails a few years ago and despite my best attempts I still find Rails more enjoyable than Node/Express, despite being a JS pro. I guess for a lot of people it boils down to "I don't like or want to learn Ruby hence Rails is dying hence Node is superior." Node is pretty cool but gently caress writing a bunch of poo poo by hand that Rails can just do for me.

My ideal architecture is a Rails API with an SPA front-end. Luckily Rails 5 agrees with me on the API front.

kayakyakr
Feb 16, 2004

Kayak is true

Noam Chomsky posted:

I'm a full-time frontend developer who picked up Rails a few years ago and despite my best attempts I still find Rails more enjoyable than Node/Express, despite being a JS pro. I guess for a lot of people it boils down to "I don't like or want to learn Ruby hence Rails is dying hence Node is superior." Node is pretty cool but gently caress writing a bunch of poo poo by hand that Rails can just do for me.

My ideal architecture is a Rails API with an SPA front-end. Luckily Rails 5 agrees with me on the API front.

I think that's where it's heading. Rails is still pretty strong to me.

I'd really like to see it do a bit more to work with JS package managers. I wish I had more time to hack on it, I'd start trying to work with that...

Adbot
ADBOT LOVES YOU

8ender
Sep 24, 2003

clown is watching you sleep
We user bower to grab our JS libs and chuck them into the asset pipeline before it hits docker. Works well.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply